Need help acessing variable from different script.

Hi all, I need to acess a bool variable from another script/class to test it in an if() statment.

public class ExampleOne
{
    public bool b = true;
}

// the class on the other script
public class ExampleTwo
{
    if(b == true)
    {
        //do something
    }
}

I must note that the scripts are atteched to 2 different game objects, but these game objects are under the same parent game object.
And public bool b = true; cannot be a static variable as the value needs to be able to change.

I hope all of this makes sense, I’ve been stuck on this for a while now and have kept putting it off as I cannot find a soloution.

Use the “.” accessor which is given by almost every programming languages.

You can use methods like GameObject.Find or FindObjectOfType to get the gameobject you want to get the class from. Then you can use the GetComponent method.

var objToGetB = GameObject.Find("nameofobject");
// You can also use gameObject.FindObjectOfType<YourClass>() to get the class itself directly.
// You can also use GameObject.FindWithTag("tag");

// Then
var myClass = objToGetB.GetComponent<YourClass>()

myClass.yourField// use it however you like

You can find more in methods here: Unity - Scripting API: GameObject in the static methods section.

Referencing variables, fields, methods (anything non-static) in other script instances:

Thank you @UncleanWizard and @Kurt-Dekker for your suloutions. Problem is solved now