Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
4 captures
13 Jun 22 - 14 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 /
  • Help Room /
avatar image
0
Question by WizardGoose · May 14 at 11:55 AM · instantiateif-statementsinstantiate prefabinstantiation

Infinite Instantiation


I am instantiating enemies. My if-statement should close itself as soon as one appears, but for some reason it repeats itself over a hundred times before stopping.


Just a note about the code, I only have this code on a single game object and don't instantiate in any other scripts, so it isn't the typical issue where clones are creating clones. Additionally, the editor correctly shows the enemyCount at any given time. Despite the fact that enemyCount increases above 1, clones are still generated.


I'm quite new to Unity and C#, so I'm not sure if there's something obvious I'm just missing or if there's some complex reasoning behind the issue. If anyone knows what I can do to fix this, please let me know, it would be greatly appreciated :)


 void Update ()
 {
     if( enemyCount<1 )
         Invoke("RespawnEnemies", 2);
 }

 void RespawnEnemies ()
 {
     enemyCount ++;
     int randomEnemy = Random.Range( 0 , enemySpawnPoints.Length-1 );
     Instantiate( enemySpawnPoints[randomEnemy] , transform.position , Quaternion.identity );
 }


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 andrew-lukasik · May 14 at 11:53 AM 0
Share

I am instantiating enemies. My if-statement should close itself as soon as one appears, but for some reason it repeats itself over a hundred times before stopping.

It is because your Update calls Invoke( nameof(RespawnEnemies) , 2 ); for 2 seconds, every frame (dozens if not hundreds of times per second). Calls to Invoke( nameof(RespawnEnemies) , 2 ); stop after 2 seconds, but delayed torrent of RespawnEnemies() calls just starts and continues for another 2s.


Delete this Update and replace it with this:

 IEnumerator Start ()
 {
     var wait = new WaitForSeconds(2);
     
     yield return wait;
     
     while( true )
     {
         if( enemyCount<1 )
         {
             int randomEnemy = Random.Range( 0 , enemySpawnPoints.Length-1 );
             Instantiate( enemySpawnPoints[randomEnemy] , transform.position , Quaternion.identity );
             enemyCount ++;
         }
 
         yield return wait;
     }
 }


2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by TheIrishKraken · May 16 at 05:32 AM

Using Invoke is causing this issue.

Call the method directly instead & you can use a countdown timer to space out the spawning of the enemies so they don't spawn in too quickly.

So if the enemyCount is less then 1 start a timer that counts down from 2 seconds. When the timer reaches 0 then run RespawnEnemies() & reset the timer.

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
avatar image
0

Answer by WizardGoose · May 16 at 06:13 PM

Thanks, I think both of these solutions work great! I hadn't thought of Evoke being the issue, so this was a ton of help :)

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

161 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 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

How to Change the Variables of an Instantiated Object? 0 Answers

Assign variables on Instantiation? 0 Answers

Change value of a static variable multiple times in one script 2 Answers

randomly shoot using number generator 0 Answers

Setting a variable on instantiation, but when the object is enabled the variable is set back to default. 0 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