Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 slayer29179 · Jun 17, 2012 at 02:17 PM · instantiateyielddelay

Delay in Instantiate only runs once

Hello all! I have a spawning system that checks the maximum spawn limit by randomising between two locations and if its below it, it will spawn more zombies.

At the moment it all works good except and the start of the game as it starts spawning it spawns the zombies at the random locations however all at once which makes them explode (which does look pretty cool) but its not what I want. So I inserted a delay using

yield WaitForSeconds(5);

however it only seems to run the delay code once then never again so it waits for 5 seconds, then spawns the exploding zombies. This is my code;

 #pragma strict
 //Integers
 static var MaxAI = 5;
 var EnemysSpawned = 0;
 static var EnemysKilled = 0;
 var spawnLocation = 1;
 
 //boolean
 var spawnForLevel = true;
 
 //Transforms
 var spawnLocationR : Transform;
 var spawnLocationL : Transform;
 var locationToSpawn : Transform;
 
 //Enemys
 var Zombie : Transform;
 
 function Update ()
 {
     if (spawnForLevel == true)
     {
         if (EnemysSpawned < Spawning_Rate.MaxAI)
         {
             randomGens();
             Delay();
             var spawnZombie = Instantiate (Zombie, locationToSpawn.position, Quaternion.identity);
             EnemysSpawned += 1;
         }
         
         if (EnemysSpawned == Spawning_Rate.MaxAI)
         {
             spawnForLevel = false;
         }
     }
 }

 //This is the delay code, it only seems to run this once then skip it every other time

 function Delay()
 {
     yield WaitForSeconds(5);
 }

 //Randomise Code
 function randomGens ()
 {
     //Position
     var randPos = Random.Range(0, 1);
     spawnLocation = randPos;
     
     switch (EnemysSpawned)
     {
         case 0:
             locationToSpawn = spawnLocationR;
         break;
         case 1:
             locationToSpawn = spawnLocationL;
         break;
     }
 }

I have also tried using LateFunction but no luck how can I make it run the code more than once? thank you!

Comment
Add comment · Show 1
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 Wolfram · Jun 17, 2012 at 07:35 PM 0
Share

Note that Random.Range(a,b); will never return b if a and b are integers. Check the documentation page. In your case it will therefore always return 0.

2 Replies

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

Answer by slayer29179 · Jun 18, 2012 at 05:42 AM

In the end I still don't know why the yield function doesn't work, so instead I created a small timer

 var Timer = 300;
 

and a boolean

 var TimerGo = false;
 

and then put in

 if (TimerGo == true)
 {
      Timer -=1;
      if (Timer < 0)
      {
           spawnEnemy = true;
           Timer = 300;
      }
 }
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 Wolfram · Jun 18, 2012 at 10:36 AM 0
Share

Note that this will cause your spawn interval to be dependent on the frame rate - faster framerate, faster spawn. (unless VSync is active)

avatar image
2

Answer by Wolfram · Jun 17, 2012 at 07:48 PM

I'm not entirely certain how Coroutines behave in UnityScript, but I'm pretty sure that your call to Delay() just spawns a coroutine that waits for 5 seconds and then ends, doing nothing. At the same time, execution continues immediately after the line Delay(), without actually delaying. So in the version of the code you posted, there shouldn't even be a delay, there will be one zombie spawned every frame.

I think the easy fix is to just insert the yield keyword before your call, compare to the documentation page. Also, you need to move the counter increment before the call to Delay, so it registers as "a zombie will be spawned", and prevents you from indefinitely spawning zombies until after the delay (because your counter would remain 0 the whole time). Move "randomGens" after the delay, for a similar reason - otherwise locationToSpawn might change in the meantime, overwriting the value for all zombies that are still in the "spawn queue".

      EnemysSpawned += 1;
      yield Delay();
      randomGens();
      var spawnZombie = Instantiate (Zombie, locationToSpawn.position, Quaternion.identity);



Comment
Add comment · Show 6 · 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 slayer29179 · Jun 17, 2012 at 09:39 PM 0
Share

thanks for the random help! that has been fixed :) (didn't notice it until you mentioned it) but the wait code the "Delay();" is linking to the function I created called delay in that function it says yield WaitForSeconds(5); which creates the delay if I put yield in your example it says it could not quarantine thanks for the help so far

avatar image Wolfram · Jun 18, 2012 at 12:10 AM 0
Share

Even after reading your comment several times, I still have no idea what you were trying to say. Please learn how to use punctuation marks, and place them within and between your sentences where they belong.

"yield" cannot be used in Update() directly. So I suggest you forget about your current Delay()-function, replace the "Delay()" in my example code by "WaitForSeconds(5)", and then put the whole block from my example into a seperate function, for example "DelayAndSpawn()". Then call that function in the body of your "if (EnemysSpawned < Spawning_Rate.$$anonymous$$axAI)" statement (and of course remove the other lines in there). Don't use "yield" when calling the function.

avatar image slayer29179 · Jun 18, 2012 at 05:19 AM 0
Share

sorry and basically the delay() function is just a created function. I could be called anything but that function contains the yield WaitForSeconds which links to it to run that delay

avatar image slayer29179 · Jun 18, 2012 at 05:21 AM 0
Share

for example I could of called it "timer();" and in that function "yield WaitForSeconds(5);" but it still doesn't run that function more than once

avatar image Wolfram · Jun 18, 2012 at 10:30 AM 0
Share

You're right, sorry about that. What you need is a flag that keeps other coroutines from launching while one is active. There are several variants to accomplish this. Starting from the code you already have and I modified, I'd suggest this:

 function Update ()
 {
     if (spawnForLevel == true)
     {
        if (EnemysSpawned < Spawning_Rate.$$anonymous$$axAI)
        {
          DelayAndSpawn();
        }
 
        if (EnemysSpawned == Spawning_Rate.$$anonymous$$axAI)
        {
          spawnForLevel = false;
        }
     }
 }
 
 //This is the delay code, it only seems to run this once then skip it every other time
 
 function DelayAndSpawn()
 {
      spawnForLevel = false; // don't spawn additional objects while we're running
      EnemysSpawned += 1;
      yield WaitForSeconds(5);
      randomGens();
      var spawnZombie = Instantiate (Zombie, locationToSpawn.position, Quaternion.identity);
      spawnForLevel = true; // we're done, ready for the next enemy
 }
Show more comments

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

How to delay a respawn? 1 Answer

Delay between each instantiation 3 Answers

Coroutines insider another coroutine. 1 Answer

WaitForSeconds Question 2 Answers

Instatiate inside OnTriggerEnter gives unwanted delay 1 Answer


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