- Home /
 
how to instantiate in array consecutive
     Transform[] meatballSlots;
     GameObject meatball;
 
     private void Update()
     {
         foreach (Transform meatballSlot in meatballSlots)
         {
             if (Input.GetKeyDown(KeyCode.Space))
             {
                 Instantiate(meatball, meatballSlot);
             }
         }
     }
 
               how can i to instantiate the "meatball" as child of "meatballSlot" consecutive, right now if i press space the "meatball" will instantiate on every "meatbalSlot", but i need instantiate will consecutive from 0, 1, 2, 3 and then when press each space
Answer by FernandoHC · Aug 08, 2018 at 03:51 PM
I can't understand exactly what you are saying, but I will assume you mean to instantiate only once meatball per space, and not all at once. If so, then use:
     Transform[] meatballSlots;
     GameObject meatball;
     int currentMeatball = 0;
 
     private void Update()
     {
 
         if (Input.GetKeyDown(KeyCode.Space))
         {
             if (currentMeatball < meatballSlots.Length)
             {
                 Instantiate(meatball, meatballSlots[currentMeatball]);
                 currentMeatball++;
             }
             else
             {
                 // No more meatballs left =(
             }
         }
     }
 
              Your answer
 
             Follow this Question
Related Questions
How to put gameObjects to the list? 4 Answers
Cycle Through GameObjects in Array Issue. 1 Answer
C# How to Find the Height of an Instantiated GameObject in an Array? 1 Answer
Prefabs instantiated from an array are keeping their public int value 1 Answer
Finding the Sum of Values of Multiple GameObjects in an Array + Variable Sized arrays 0 Answers