get same var from multiple different scripts

hi all

im trying to get to a var that placed in different scripts.
for example:

state = true;


state = true;


gameObject.GetComponent(scriptA/B).state = false;

is it possible to get both “state” vars from scriptMaster.js with arrays and for loop
in some way?

thank u

What you can do is create an abstract class that contains a protected bool “state” and have scriptA and scriptB inherit from it.

Here’s one way to do it using C#:

public abstract class Statable : MonoBehaviour 
  public bool state;

public class ScriptA : Statable {}

public class ScriptB: Statable {}

public class ScriptMaster : MonoBehaviour 
  public void SomeFunction()
    GetComponent<Statable>().state = false; //Gets any component that inherits Statable and sets its state to false

Note that in this example, you’ll get a NullReference on ScriptMaster if there is no Statable attached to the same game object, and if there are multiple Statable scripts it will only set one of their states to false.