Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by DLewIV · Jun 24, 2016 at 05:01 AM · c#spawningwaitforsecondsdelay

I want to create a pause in between spawning objects in a row

I have already been able to spawn my cubes in a row. Now I want to create a small delay so you can see the cubes individually falling. I have tried making a coroutine where it waits, and I have read multiple of these qustion forums and nothing has worked. I was very confident in the code below, but that does not work as well. Any help would be amazing.

 using UnityEngine;
 using System.Collections;
 
 public class CubeSpawn1 : MonoBehaviour
 {
 
     public Vector3 ObjectSpawnPosition;
     public GameObject obj;
     private float nextSpawn;
     public float fireRate;
     public float NumBlks;
     public float Depth;
 
     // Use this for initialization
     void Start()
     {
 
     }
 
     // Update is called once per frame
     void Update()
     {
         if (Input.GetKey("s") && Time.time > nextSpawn)
         {
             nextSpawn = Time.time + fireRate;
             for (int j = 0; j < Depth; ++j)
             {
                 for (int i = 0; i < NumBlks; ++i)
                 {
                     Instantiate(obj, ObjectSpawnPosition, Quaternion.identity);
                     ObjectSpawnPosition.x += 1;
                     StartCoroutine(HERE());
                 }
                 ObjectSpawnPosition.x -= NumBlks;
                 ObjectSpawnPosition.z += 1;
             }
             ObjectSpawnPosition.z -= NumBlks;
         }
 
     }
     public IEnumerator HERE()
     {
         float x = 1;
         yield return new WaitForSeconds(1.0f); //this stops the coroutine for 1 second.
         x += 1;
     }
 }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Eudaimonium · Jun 24, 2016 at 09:42 AM

Close, but a few rookie mistakes :)

This should work, I have only redited it in text editor so I am not 100% sure:

      // Update is called once per frame
      void Update()
      {
          if (Input.GetKey("s") && Time.time > nextSpawn)
          {
              nextSpawn = Time.time + fireRate;
              
              for (int j = 0; j < Depth; ++j)
              {
                  StartCoroutine(HERE());
 
                 
                  ObjectSpawnPosition.x -= NumBlks;
                  ObjectSpawnPosition.z += 1;
              }
              ObjectSpawnPosition.z -= NumBlks;
          }
  
      }
      public IEnumerator HERE()
      {
         
          for (int i = 0; i < NumBlks; ++i)
                  {
                      Instantiate(obj, ObjectSpawnPosition, Quaternion.identity);
                      ObjectSpawnPosition.x += 1;
                      yield return new WaitForSeconds(1.0f); 
                  }
          
      }


In short, you shoult not use a coroutine as a "wait timer", your coroutine has to do the spawning and waiting itself. Your task is basically to run it.

You can change this probably to have both of your loops inside the coroutine and all you have to do is run it after "nextSpawn" increase.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image DLewIV · Jun 24, 2016 at 03:04 PM 0
Share

Thank you! That almost worked I just had to do some small tweaking but I got it to work. I really appreciate the help.

avatar image
1

Answer by Mindmapfreak · Jun 24, 2016 at 04:11 PM

You could set the instantiated objects to non-active and add them to a queue, then activate them successively. You can compute the time between activations like you do for the initial spawning. This approach would look like this:

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class CubeSpawn1 : MonoBehaviour
 {
     public Vector3 ObjectSpawnPosition;
     public GameObject Obj;
     private float nextBulk = 0.0f;
     private float nextSpawn = 0.0f;
     public float FireRate;
     public float TimeBetweenSpawns;
     private Queue<GameObject> objectsToSpawn;
     public int NumBlks;
     public int Depth;
 
     // Use this for initialization
     void Start()
     {
         this.objectsToSpawn = new Queue<GameObject>();
     }
 
     // Update is called once per frame
     void Update()
     {
         if (Input.GetKey("s") && Time.time > nextBulk)
         {
             nextBulk = Time.time + FireRate;
             for (int j = 0; j < Depth; ++j)
             {
                 for (int i = 0; i < NumBlks; ++i)
                 {
                     GameObject curObj = Instantiate<GameObject>(Obj);
                     curObj.transform.position = ObjectSpawnPosition;
                     curObj.transform.localRotation = Quaternion.identity;
                     curObj.SetActive(false);
                     this.objectsToSpawn.Enqueue(curObj);
                     ObjectSpawnPosition.x += 1;
                 }
                 ObjectSpawnPosition.x -= NumBlks;
                 ObjectSpawnPosition.z += 1;
             }
             ObjectSpawnPosition.z -= NumBlks;
         }
 
         if(Time.time>nextSpawn && this.objectsToSpawn.Count>0)
         {
             nextSpawn = Time.time + TimeBetweenSpawns;
             this.objectsToSpawn.Dequeue().SetActive(true);
         }
 
     }
 }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Wait for seconds question 1 Answer

Waiting for fractions of a Second 1 Answer

Multiple Cars not working 1 Answer

check alpha of gameObject C# 3 Answers

Distribute terrain in zones 3 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges