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 SeanWink · Jul 04, 2017 at 06:06 PM · unity 5coroutineloopdelaystart

I'm having trouble with a coroutine delay

shouldnt this work? I put a delay before the Spawnloop delay. contents are in the NewDelay function.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class ObstacleSpawner : MonoBehaviour 
 
 {
 
     public int obstacles_min = 1;
     public int obstacles_max = 4;
     public float obstacles_range= 10;
     public float obstacles_deviation_min = -3;
     public float obstacles_deviation_max = 3;
     public Transform playerTransform;
     public GameObject obstaclePrefab;
     public float obstacleLifetime = 4f;
     public float spawnDelay = 3;
     public float BeginingDelay = 4;
 
 
 
     // Use this for initialization
     void Start ()
     {
         StartCoroutine ("NewDelay");
             StartCoroutine ("SpawnLoop");
         
     }
     
     // Update is called once per frame
     void Update () 
 
     {
         
     }
 
     private void SpawnObstacles()
     {
         
         int randomNumber = Random.Range (obstacles_min, obstacles_max); // Picks a number within min and max and store in randomNumber
         for (int i = 0; i < randomNumber; i++)// i = 0 if i is less than the random number stored increment i++ until its reached the value of the random number
         {
             Vector3 randomPos = new Vector3 (playerTransform.position.x + Random.Range (obstacles_deviation_min, obstacles_deviation_max), playerTransform.position.y, playerTransform.position.z + obstacles_range);
             GameObject obstacle = Instantiate (obstaclePrefab, randomPos, Quaternion.identity) as GameObject;
             Destroy (obstacle, obstacleLifetime);
         }
     }
 
     private IEnumerator SpawnLoop()
     {
         while (true)
         {
             SpawnObstacles ();
             yield return new WaitForSeconds (spawnDelay);
             //print ("spawn delayed");
         }
         yield return null;
 
     }
     private IEnumerator NewDelay()
     {
         yield return new WaitForSeconds (BeginingDelay);
     }
 
         
 }
 
 
 
 
 
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
2

Answer by merkaba48 · Jul 04, 2017 at 06:46 PM

Your NewDelay coroutine isn't actually doing anything. It runs, waits a bit, then runs again; but what's it doing?

I'm going to make an assumption that you're expecting the Start() method to wait until NewDelay returns before running SpawnLoop - that's now how it works. NewDelay will run, then as soon as it returns, SpawnLoop will run - they do not affect each other. You wouldn't want Start() to wait for NewDelay to finish its WaitForSeconds anyway, as that would lock the entire game up until it's complete (other scripts will only be run when Start(), or any method, has finished executing).

One solution is to use the Invoke method and a new function that simply starts the SpawnLoop , e.g. new function StartSpawning,

 void StartSpawning() {
     StartCoroutine("SpawnLoop");
 }

Then, in your Start, replace everything there with,

 Invoke("StartSpawning", BeginingDelay);


Another, perhaps better solution is to keep things how they are, but remove everything to do with NewDelay, then modify SpawnLoop thusly:

 private IEnumerator SpawnLoop()
      {
          yield return new WaitForSeconds(BeginingDelay); // New line
          while (true)
          {
              SpawnObstacles ();
              yield return new WaitForSeconds (spawnDelay);
              //print ("spawn delayed");
          }
          yield return null;     
      }

This means the first time the Coroutine is run, it waits for BeginingDelay (btw it's spelt "Beginning" :)) seconds, and then carries on to the rest of the Coroutine.

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 SeanWink · Jul 04, 2017 at 11:07 PM 0
Share

Thanks! I don't know why I didnt just think of putting a delay in the spawnloop function. Great Thinking!

avatar image
0

Answer by Cornelis-de-Jager · Jul 05, 2017 at 01:14 AM

Hi SeanWink,

The problem is in your start function.

 void Start ()
      {
         StartCoroutine ("NewDelay");
         StartCoroutine ("SpawnLoop");         
      }

The issue is that the StartCoroutine function doesn't Add a delay. It creates a new function that runs at the same time as your start function. So everything in your Start function gets executed instantly. To solve this do the following:

 void Start ()
          {
             StartCoroutine ("NewDelay");
             // Remove the coroutine from here 
          }
 
 private IEnumerator NewDelay()
      {
          yield return new WaitForSeconds (BeginingDelay);
          StartCoroutine ("SpawnLoop");     // Add it here after the delay 

}

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

132 People are following this question.

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

Related Questions

Coroutine gets stuck randomly? 1 Answer

Delay quitting and finish some coroutines 1 Answer

Coroutines WaitForSeconds – uneven spacing 2 Answers

How do I get into the data returned from a UnityWebRequest ? 2 Answers

Wait for 1/4 seconds before executing the 'if' statement 2 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