Randomize gameobjects

hi everyone

i have multiple gameobjects in my scene (24 gameobjects), i made it in randomly showing the half of them. but the issue is that they appear outside my scene.the strange thing is that the position values of one gameobject is exactly the same of the original gameobject but it’s not in the same position

i’m using this code that i attched to an empty gameobject

public GameObject[] aliments
        ;

    // Use this for initialization
    void Start () {
   

        for (int i =0; i<aliments.Length/2; i++) {
            int randomInt = Random.Range (0, aliments.Length - 1);
       
            Instantiate (aliments [randomInt]);


        }
    }

Are your “aliments” object prefabs, or are they objects in the scene?

objects in the scene. should i haven’t use Instantiate ?

Instantiate creates a new copy of a GameObject.

i noticed that . is there any solution that doesn’t have to create a copy of my gameobject and randomly shows a number them not all of them

The best thing might be to enable / disable them as needed. You can use gameObject.SetActive(false) to disable / enable them.

i don’t see how enabling or disabling them helps to randomly showing some of them

for (int i=0;i< aliments.Length;i++) {
ailments[i].SetActive(Random.value > 0.5f);
}
1 Like

When a GO is disabled it won’t be rendered.

If they are disabled they wont be visible or interact with anything in your scene. Just start them out disabled and then enable half of them randomly.

it works like a charm, thanks :slight_smile:

thank you all for your answers, i’ve tried the code of StarManta and it works