Unity3D - get component

What is the easiest way to getcomponent in Unity3D C#?

My case:

GameObject gamemaster. 
//C# script MainGameLogic.cs(attached to gamemaster). 
A boolean backfacedisplayed(in MainGameLogic.cs).
A function BackfaceDisplay()(in MainGameLogic.cs).

Another C#script FlipMech.csthat I need to check from MainGameLogic.cs if(backfacedisplayed == TRUE)is what I will call BackfaceDisplay()from MainGameLogic.cs. How can I do this in C #?

In js, it's pretty straight forward. In FlipMech.js:

//declare gamemaster
var gamemaster:GameObject;

Then where I need to:

if(gamemaster.GetComponent(MainGameLogic).backfacedisplayed==true)
{
    gamemaster.GetComponent(MainGameLogic).BackfaceDisplay();
}

But it seems to C#be more complex than that.

+4
source share
2 answers

in C #, you will get the component using this,

if(gamemaster.GetComponent<MainGameLogic>().backfacedisplayed==true)
    {
        gamemaster.GetComponent<MainGameLogic>().BackfaceDisplay();
    }
+5
source

, . script Unity3d, . , .

using UnityEngine;
using System.Collections;
using Devdog.InventorySystem;

namespace Devdog.InventorySystem.Demo
{
  public class changeStat : MonoBehaviour {
    // ... rest of the code including the gameObject.GetComponent<>();
  }
}
+1

Source: https://habr.com/ru/post/1523479/


All Articles