c# Searching for all instances of a script and determining which one to use

I have several of the same script in my game attached to different objects. Each script has has its own unique set of variables. This script has a bool on it but on only one of the instances of the script will it be set to true at any given time. How would I search my scene for every instance of this script and only use variables from the one that has the bool marked as true?

well…you can store a reference to the one with the bool as a static variable in the class…

I feel like an idiot for asking this but, could you show me an example. I am not very good with the “coding terminology”. It is easier for me to understand if I see it rather than reading it.

getComponentsInChildren() from scene root then loop through it and find the one with bool set to true

I am not sure that I follow. Scene root?

a static variable is a class variable as opposite to an instance variable…which means you can access it directly by referencing the class.

So something like this

public class MyClass : Monobehaviour
{
    public static bool myBool;
}

Then you can use MyClass.myBool to access it.

Trying to do too much with too little theory can lead to a lot of trouble and lead to very frustrating situations…so I recommend to do some pure c# tutorials. Not that I’m a coding super pro…but yes I made that mistake myself in the beginning.

Also if the bool is only used to find an instance that is let say selected/active/important…you can replace it with reference to the instance.

So if we pretend that we want a reference to a selectedObject…that we select with the function SelectGameObject()

public class MyClass : Monobehaviour
{
    public static GameObject selectedObject;

   public void SelectGameObject()
  {
      selectedObject=this.gameObject;
     //more exciting code here that does stuff
  }

Then you can use selectedObject to access it.

So I could do something similar to that by doing this:

public static MyScript myScript;

Then assign the script with the bool set to true to myScript. That should allow me to use the correct instance of the script at any given time by calling a find method.

public void FindScript(){
        //something that cycles through all existing instances of MyScript
        if (myBool == true) {
            myScript = //Whatever instance of the script ;
        }
    }

Right now I just need help filling in the blanks.

to assign the script reference you use “this”.

myscript=this;

but I would assign the instance ref at the same time as you assign the bool…so you don’t have to look for it.

so you would get MyScript.myScript…

It might help if I put what I am doing in perspective. There are going to be about 10 characters on a battle field that the player can individually control. Upon selecting one of the 10 characters I want an IsActive bool on a CharacterSheet to turn on for the selected character and turn off for the last character selected. That bool then tells another script named “Combat” what Instance of the CharacterSheet to use to retrieve the correct stat variables during turn based combat because the IsActive bool is enabled. Hopefully this knowledge will help us get to a solution faster.

bump

you don’t need an isActive bool. just a static MyScript SelectedInstance. when you select a character you set SelectedInstance to the character that was selected.

no looping needed.

you can even go further and make the static field a property with a setter that also triggers an event so that your Combat script is notified when it changes and thus recollects the stats from SelectInstance and repaints them onto the screen. that why the Combat script won’t have to poll the other script every frame.