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

Move 10 GameObjects, one at a time

Discussion in 'Scripting' started by skinner92, Oct 30, 2017.

  1. skinner92

    skinner92

    Joined:
    Feb 23, 2014
    Posts:
    112
    Hello all. After a long break (some years) I have decided to come back and try to create some stuff.

    I'm a bit rusty in Unity, though. What I'm trying to achieve actually seems fairly simple.

    I have an array of 10 GameObjects (Spheres), and I have a method called "Move" which will move a given sphere, wherever. I'm trying to move one sphere at a time, every 5.0 seconds.

    Code (csharp):
    1. GameObject[] spheres = new GameObject[10];
    2. float timeBetweenLaunches = 5.0f;
    3.  
    4. for (int i = 0; i < 10; i++)
    5. {
    6.      StartCoroutine(Utils.Move(spheres[i], timeBetweenLaunches));
    7. }
    This code moves all 10 spheres at the same time.

    I know why this code doesn't work (the for-loop executes extremely fast, there is really no time between loops). However I can't come up with a solution, even though I'm sure it's pretty simple...

    How can I solve this problem?

    Thank you in advance! :)
     
    Last edited: Oct 30, 2017
  2. fire7side

    fire7side

    Joined:
    Oct 15, 2012
    Posts:
    1,819
    In your coroutine, start it with:
    yield return new WaitForSeconds(5);
    Then the code.

    or do it after the code is complete.

    or you can make the loop code a coroutine, and put the delay at the end of each loop. Is that c# or unity script? The coroutine code is a little different for unity script, I think.
     
    Last edited: Oct 30, 2017
  3. skinner92

    skinner92

    Joined:
    Feb 23, 2014
    Posts:
    112
    Thank you for the answer. I'm already doing that:

    (in Utils.cs):
    Code (csharp):
    1.  
    2. public static IEnumerator Move(GameObject sphere, float time)
    3. {
    4.     yield return new WaitForSeconds(time);
    5.  
    6.     // Here goes code for translating sphere, not relevant
    7. }
    8.  
     
  4. skinner92

    skinner92

    Joined:
    Feb 23, 2014
    Posts:
    112
    Initially, each Sphere is at rest. I just need to move them one at a time. Would you give me a bit more detail on how to implement your solution fire7side?
     
  5. johne5

    johne5

    Joined:
    Dec 4, 2011
    Posts:
    1,133
    here is a sample of how to run a Coroutine

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class ExampleCode : MonoBehaviour
    5. {
    6.  
    7.     GameObject[] spheres = new GameObject[10];
    8.     float timeBetweenLaunches = 5.0f;
    9.    
    10.     void Start()
    11.     {
    12.         StartCoroutine(Example());
    13.     }
    14.  
    15.     IEnumerator Example()
    16.     {
    17.         for (int i = 0; i < spheres,Length; i++)
    18.         {
    19.              Move(spheres[i]);
    20.              yield return new WaitForSeconds(timeBetweenLaunches);
    21.         }
    22.     }
    23.    
    24.     void Move(GameObject sphere)
    25.     {
    26.       //code to move the shpere
    27.     }
    28. }
     
  6. skinner92

    skinner92

    Joined:
    Feb 23, 2014
    Posts:
    112
    Thank you @johne5 ! I will try that out tomorrow and come back with updates.
     
  7. johne5

    johne5

    Joined:
    Dec 4, 2011
    Posts:
    1,133
    Did you get it working?
     
  8. skinner92

    skinner92

    Joined:
    Feb 23, 2014
    Posts:
    112
    I did Johne! I will paste a GIF as soon as I get home.
     
  9. skinner92

    skinner92

    Joined:
    Feb 23, 2014
    Posts:
    112
    There we go, I attached the GIF to this reply.

    In reality I was not using Spheres, but Fruits, in my chop-only-that-kind-of-fruit type of game :p Your help was very helpful, thanks!
     

    Attached Files: