Iterating and changing through list of variables via script instance?

Let’s stay I have a bunch of bools:
variable_1 variable_2 variable_3 ... variable_30

They are public variables that belong to a different script/class but I can set/get them via instance of a script. What I want is to iterate through all of them (30 fixed) and set each to false.

I’ve got a suggestion to:
bool[] variable_; for(int i=0;i<30;i++) variable_*=value;* **
But I cant use Class.instance.variable_* here.
EDIT: what I want is to set specific group of variables to FALSE or TRUE on demand without this nonsense:
_
*_ <em>*variable_1 = true;*</em> <em>*variable_2 = true;*</em> <em>*variable_3 = true;*</em> <em>*variable_4 = true;*</em> <em>*variable_5 = true;*</em> <em>*variable_6 = true;*</em> _*...*_ <em>*variable_99 = true;*
Part of their names is just incrementing number, so I wanna loop through them and set values when needed.
ANSWER (simple and elegant):
_*winforms - How can I compose variables names through loop in C#? - Stack Overflow

You need to keep references to the instances properties.
You could do that with Reflection, but it’s not ideal, and not supported on every platform.

Now, this sounds like strong coupling between a higher module and lower modules.
You may want to invert the flow.

You could for example, raise an event when a bool property changes, and have subscribers change theirs accordingly.

public class Manager : MonoBehaviour
{
    public delegate void StateChange (bool state);
    public event StateChange OnStateChanged;
    
    void Update ()
    {
        // change the global state when you want
        if (OnStateChanged != null) // make sure their are listeners
            OnStateChanged (true); // then raise the event
    }
}

public class Subscriber : MonoBehaviour
{
    Manager manager;
    bool localState;
    void Awake ()
    {
        manager = FindObjectOfType<Manager>(); // find the Manager, you can also use a Singleton pattern to save the calls to FindObjectOfType
        manager.OnStateChanged += UpdateState; // subscribe the method to the event
    }
    void UpdateState (bool state)
    {
        localState = state; // update the state when the event is raised
    }
}

You could also use interfaces instead of events, in which case, the Manager finds the interface implementing instances and calls their UpdateState method.

This really depends on the context. Tell us more about what you want to do if you need more.

If you’re not comfortable with Pub/Sub or find looping over a collection of bools slows your performance by a fair amount, you can actually get this done without needing a loop. You can derive ICollection and set its generic type to a custom type that links a string to a bool value. Then, you can write a custom indexer to set and retrieve values based on a string. Enforce unique IDs by implementing ICollection’s methods with Linq (the type names are probably pretty bad, but will still do the job):

public class KeyedBool
{
    bool   m_value;
    string m_id;

    public KeyedBool(string id)
    {
        m_id = id;
    }

    public KeyedBool(string id, bool value)
    {
        m_id    = id;
        m_value = value;
    }

    public bool   Value { get { return m_value; } set { m_value = value; } }
    public string ID    { get { return m_id; } }
}

public class BoolValueRetriever : ICollection<KeyedBool>
{
    private ICollection<KeyedBool> m_collection = new List<KeyedBool>();

    public bool this[string id]
    {
        get
        {
            if (m_collection.Where(b => b.ID == id).SingleOrDefault() == null)
                throw new IndexOutOfRangeException("ID not present in collection.");

            return m_collection.Where(b => b.ID == id).SingleOrDefault().Value;
        }
        set
        {
            if (m_collection.Where(b => b.ID == id).SingleOrDefault() == null)
                throw new IndexOutOfRangeException("ID not present in collection.");

            m_collection.Where(b => b.ID == id).SingleOrDefault().Value = value;
        }
    }

    public int Count { get { return m_collection.Count; } }
    public bool IsReadOnly { get { return m_collection.IsReadOnly; } }

    public void Add(KeyedBool item)
    {
        if (m_collection.Where(k => k.ID == item.ID).Count() > 0)
            throw new Exception("ID already present in collection.");

        m_collection.Add(item);
    }

    public void Clear()
    {
        m_collection.Clear();
    }

    public bool Contains(KeyedBool item)
    {
        return m_collection.Contains(item);
    }

    public void CopyTo(KeyedBool[] array, int arrayIndex)
    {
        m_collection.CopyTo(array, arrayIndex);
    }

    public bool Remove(KeyedBool item)
    {
        return m_collection.Remove(item);
    }

    public IEnumerator<KeyedBool> GetEnumerator()
    {
        return m_collection.GetEnumerator();
    }

    IEnumerator IEnumerable.GetEnumerator()
    {
        // This is probably implemented incorrectly, not sure.
        return m_collection.GetEnumerator();
    }
}

Toss those in either the same file or use separate files. Doesn’t matter, but to consume the object:

using UnityEngine;

public class ExampleClass : MonoBehaviour {
    void Awake() {
        BoolValueRetriever bvr = new BoolValueRetriever();

        bvr.Add(new KeyedBool("variable_1"));
        bvr.Add(new KeyedBool("variable_2", true));

        // Uncommenting this line will cause an Exception to be thrown.
        // bvr.Add(new KeyedBool("variable_2", true));

        // Uncommenting this line will cause an IndexOutOfRange Exception to be thrown.
        // Debug.Log(bvr["variable_3"]);

        bvr["variable_1"] = true;

        if (bvr["variable_1"])
            Debug.Log("variable_1 is true");

        if (bvr["variable_2"])
            Debug.Log("variable_2 is true");
    }
}

You can extend KeyedBool to accept a reference to another object’s value as well.