I have a script in which i reference an object through its tag. I need this script to check a bool in that object, how could i do that, if its possible?
1 Answer
1Using GetComponent<scriptwithboolean> u can request the script attached to your game object.
Another (cleaner) way is to use FindObjectOfType this looks for an object in your scene that has your boolean script, this way u cant make spelling mistakes and makes the code a bit faster.
GameObject go = GameObject.FindGameObjectWithTag("booleanobject");
scriptwithboolean script = go.GetComponent<scriptwithboolean >();
Debug.Log(script.myBool);
or
scriptwithboolean boolscript = FindObjectOfType<scriptwithboolean>();
Debug.Log(boolscript.myBool);
I did the second one, the
– Diamantinessscriptwithboolean boolscript = FindObjectOfType(); Debug.Log(boolscript.myBool);but im not sure what the first "scriptwithboolean" is, as in, is it a GameObject, or what variable should i save it as. Thanks for the reply to the question!