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.
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.
in c#, you will get the component using this,
if(gamemaster.GetComponent<MainGameLogic>().backfacedisplayed==true)
{
gamemaster.GetComponent<MainGameLogic>().BackfaceDisplay();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With