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. Dismiss Notice

How to generate prefabs of the random map one by one

Discussion in 'Scripting' started by imposter_syndrom_incarnated, Sep 17, 2020.

  1. imposter_syndrom_incarnated

    imposter_syndrom_incarnated

    Joined:
    May 1, 2020
    Posts:
    51
    Apologies in advance if the question seems silly as I am a very new Unity user.
    This is the code for the random generation of the map i am working with
    Code (CSharp):
    1. public class LevelGeneration : MonoBehaviour
    2.     {
    3.         public GameObject[] objArray;
    4.         void Start()
    5.         {
    6.             //generate a random object from our array (of prefabs of sprites) using this variable
    7.             int ranNumBwZeroAndArrayLen = Random.Range(0, objArray.Length);
    8.             //Instantiate gets the object that has the index equal to the random variable
    9.             //the second parameter is the location of the new object
    10.             //the third means the object wil be generated with no rotation
    11.             Instantiate(objArray[ranNumBwZeroAndArrayLen], transform.position, Quaternion.identity);
    12.  
    13.         }
    14.     }
    which produces a random map as such
    upload_2020-9-17_14-24-54.png
    upload_2020-9-17_14-25-18.png
    upload_2020-9-17_14-25-45.png
    Just as the pictures above, different combinations will be produced.
    My question is: how can I modify the code/project to make sure that this random sequence is displayed one by one?
    Any suggestion will be much appreciated!
     
  2. LilFire

    LilFire

    Joined:
    Jul 11, 2017
    Posts:
    74
  3. imposter_syndrom_incarnated

    imposter_syndrom_incarnated

    Joined:
    May 1, 2020
    Posts:
    51
    Thank you!
    The problem seems to be now that I realized that Instantiate() in my code
    Code (CSharp):
    1.             Instantiate(objArray[ranNumBwZeroAndArrayLen], transform.position, Quaternion.identity);
    2.  
    is actually looping the objArray that has the prefabs.
    How do I manually do this loop with each element of the array showing one by one so that I can insert the coroutine method in it?
    Or, how do I loop the Instantiate() inside the coroutine method?
    I couldn't find anything to learn the connection between Instantate() and coroutines used together yet :oops:
     
  4. Vryken

    Vryken

    Joined:
    Jan 23, 2018
    Posts:
    2,106
    Code (CSharp):
    1. IEnumerator Example() {
    2.    WaitForSeconds wait = new WaitForSeconds(1f);
    3.  
    4.    for(int i = 0; i < yourArray.Length; i++) {
    5.       Instantate(yourArray[i]);
    6.       yield return wait;
    7.    }
    8. }
    The above example will instantiate every object in an array on a 1-second delay between each loop.
    You can change the parameter in the
    WaitForSeconds
    constructor to increase/decrease the delay.
     
  5. imposter_syndrom_incarnated

    imposter_syndrom_incarnated

    Joined:
    May 1, 2020
    Posts:
    51
    thank you so much!!:D:D:D