Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unity3D - get component

What is the simplest 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.cs, which I need to check from MainGameLogic.cs if(backfacedisplayed == TRUE), I will call BackfaceDisplay()from MainGameLogic.cs. How can I do it in C#?

In js is rather straight forward. In FlipMech.js:

//declare gamemaster
var gamemaster:GameObject;

Then wherever I need:

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

But it seems like C# is way more complicated that this.

like image 990
sooon Avatar asked Jan 24 '14 08:01

sooon


People also ask

How do I reference a component in Unity?

Simply attach your script with the public reference to a Game Object in your scene and you'll see the reference in the Inspector window. Then you can drag any other object that matches the type of your public reference, to this reference in the Inspector and you can use it in your script.


1 Answers

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

if(gamemaster.GetComponent<MainGameLogic>().backfacedisplayed==true)
    {
        gamemaster.GetComponent<MainGameLogic>().BackfaceDisplay();
    }
like image 129
Nick Avatar answered Sep 20 '22 17:09

Nick