Search Unity

Avctivate bool from other gameobject trow an array

Discussion in '2D' started by Helladah, Sep 28, 2019.

  1. Helladah

    Helladah

    Joined:
    May 5, 2018
    Posts:
    254
    Hi, im trying to get activate a bunch of booleans value, every one of them its at the same script but on diferent gameObjects, here its my script, but it dosent seems to works

    NullReferenceException: Object reference not set to an instance of an object




    public void ActPat()
    {
    for (int s = 0; s < x.Length; s++)
    {
    patas[s].GetComponent<script>().active = true;
    }
    }
    }
     
    Last edited: Sep 28, 2019
  2. ZliVuk

    ZliVuk

    Joined:
    Mar 24, 2018
    Posts:
    61
    You need to get all gameObjects with that script and then on that array do above for each gameObject what you did above.
    First get all gameObjects by tag for example:
    Code (CSharp):
    1.  
    2. private GameObject[] allGameObjectsWeNeed;
    3. public void Start () {
    4.      allGameObjectsWeNeed = GameObject.FindGameObjectsWithTag("My Tag");
    5. }
    6.  
    then iterate and set bool where needed:
    Code (CSharp):
    1.  
    2. foreach (GameObject singleGameObject in allGameObjectsWeNeed)
    3. {
    4.      singleGameObject.GetComponent<script>().active = true;
    5. }
    6.  
     
    MisterSkitz likes this.