- Home /
Accessing a boolean from another script with GetComponent (USING C#!)
I need this ASAP. Just can't find over the entire internet a solution for this. I'm kinda new using Unity but I would really appreciate if someone answer my question the way I want, not suggesting other stuff instead.
Just if there is no way of accessing a boolean value from another script WITH GetComponent I would like to know other solutions.
Thank you!
Answer by syclamoth · Nov 09, 2011 at 03:46 AM
So you have a script on one object called
public class ThisHasABoolean : MonoBehavioiur
{
public bool onOrOff;
}
Then in your other script, you have a reference to the gameObject that first script is attached to, and you use
otherObject.GetComponent<ThisHasABoolean>().onOrOff = true;
This sets the other boolean to true!
Disclaimer- this only works assu$$anonymous$$g that other object only has one component of that type. Otherwise, it's a bit more complicated, but we won't get into that unless you specifically need it.
Your answer
Follow this Question
Related Questions
Can someone translate C# to Javascript? 2 Answers
GetComponent keeps returning null 3 Answers
Can I access components scripts? 3 Answers