Home > Net >  Select random object from an array and change a boolean variable on the selected object
Select random object from an array and change a boolean variable on the selected object

Time:05-20

This code picks GameObjects with the tag "Coisas" and puts them on a array, then this GameObject gets picked randomly and the it gets printed.

    ChangeSprit changeSprit;
    public GameObject player;

    GameObject[] coisas;

    void Awake(){
        changeSprit = player.GetComponent<ChangeSprit>();
    }

void Pick(){
        coisas = GameObject.FindGameObjectsWithTag("Coisas");
        int randomIndex = Random.Range(0, coisas.Length);
        player = coisas[randomIndex];
        print (player.name);
    }

What i want to do is to pick a GameObject randomly, and them change a boolean variable inside this other code "ChangeSprit", to true. But i can't wrap my head around this problem.

What you guys think the solution could be?

CodePudding user response:

coisas[randomIndex].GetComponent<ChangeSpirit>().YourBooleanProperty = true

replace YourBooleanProperty with the actual property you want to set to true

  • Related