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 dvineinfekt · May 05, 2012 at 12:34 AM · spawnarraysinfinitewavesarray-out-of-range-except

How to prevent infinite loop when Array Index Is Out Of Range

Basically, my problem is that I'm using variable "i" to flip through an array of spawn points. When i becomes greater than the amount of spawn points available, "i" obviously leaves the range of the spawn point array. My solution is to simply reset "i" inside the scope of the for loop, but this doesn't seem to solve my problem. What's going on?

 function spawnWave()
 {
     if(waveActive)
     {
         for (var i=0; i<=waveNumber; i++)
         {
             if (i>8)
             {
                 i = 0;
                 print(i);
             }
             Instantiate(enemy1, Vector3(spawnArray[i].transform.position.x, spawnArray[i].transform.position.y, spawnArray[i].transform.position.z), Quaternion.identity);
             //something to delay spawning for more than six
         }
         waveActive = false;
         needSpawn = false;
     }
     else
     {
         yield WaitForSeconds(1);
         waveActive = true;
     }
 }
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 moghes · May 05, 2012 at 08:04 AM

you can use Invoke("callYourFunction", 3) for example, it will call the function in 2 seconds. In your case you can change the value of waveActive = true; inside a function and call the function from Invoke, hope it helps!

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 aldonaletto · May 05, 2012 at 12:46 AM

If you want to iterate through all elements of spawnArray, it's easier to do the following:

    for (var spawnPt in spawnArray){
      Instantiate(enemy1, spawnPt.transform.position, Quaternion.identity);
    }

But if you need the index, use the array length in a regular for loop:

    for (var i=0; i < spawnArray.length; i++){
      Instantiate(enemy1, spawnArray[i].transform.position, Quaternion.identity);
    }

Anyway, you don't need to create a new Vector3 with the position values - position itself is a Vector3, thus you can use it directly.

Comment
Add comment · Show 2 · 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 dvineinfekt · May 05, 2012 at 03:34 AM 0
Share

hmmm....out of curiousity, is there a way to have it step through the array slower? I've tried "WaitForSeconds(3)" for instance, but it doesn't seem to do anything. Adding "yield" seems to create another infinite loop. Think you can help me out again? And thanks for answering my previous question!

avatar image aldonaletto · May 05, 2012 at 10:29 AM 0
Share

It's easy in JS:

    for (var i=0; i < spawnArray.length; i++){
      Instantiate(enemy1, spawnArray[i].transform.position, Quaternion.identity);
      yield WaitForSeconds(3.0); 
    }
But you must understand how coroutines work: when you call a coroutine, it starts running and returns to the caller when the first yield is encountered - but continues running in the background, being resumed automatically each frame until its completion. You should thus make sure to not call the coroutine again until it ends, or multiple coroutine instances will run at the same time, crowding your level with tons of enemies.
A common approach is to use a boolean flag set at the coroutine start and cleared upon return:

private var spawning = false;

function spawnWave(){ if (spawning) return; // abort calls while spawnWave is running spawning = true; // set flag "spawnWave is running" if (waveActive){ ... } else { ... } spawning = false; // clear flag right before returning }

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Spawn Multiple Monsters 1 Answer

Why does my method "Vector2 SpawnPosition()" cause erratic behavior? 1 Answer

I'm getting an index out of range error even though my arrays are filled with elements? 2 Answers

WaveSpawn Script. Help 0 Answers

Unable To spawn Multiple Objects 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