Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

how would you instantiate each gameobject of a list once?

Discussion in 'Scripting' started by simiji, Jul 17, 2022.

  1. simiji

    simiji

    Joined:
    Sep 21, 2017
    Posts:
    8
    hey! I have a question, how would you instantiate each gameobject of a list once?
    Let's imagine; I have A, B and C, I want to make them each appear randomly but only once so :BCA or ACB
    and not : BBC or AAB

    here is my code :

    for(int i=0; i< myPrefab.Length; i++)
    {
    if (number < myPrefab.Length)
    {
    CarteDuJour = myPrefab[Random.Range(0,3)];
    Instantiate( CarteDuJour, PointSpawn[0].position, Quaternion.identity);
    Debug.Log(myPrefab.Length);
    number = number+1;
    }
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,780
    Shuffle the list first, then iterate the shuffled list in linear order, just as you would draw from a deck of cards.
     
    Chubzdoomer likes this.
  3. simiji

    simiji

    Joined:
    Sep 21, 2017
    Posts:
    8
    Oh thanks !