Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading