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 POLYGAMe · Sep 19, 2014 at 01:44 AM · crashwhile loop

Why would this while loop crash Unity?

I have the following code. It runs fine when I don't put it inside a while loop, but I need to as I need to try spawning another car if the one attempted is active already. I can't see anywhere that this would cause an infinite loop...

It's being called in OnTriggerEnter, not within update.

 function SpawnCar()
 {
     while (!bCarSpawned)
     {
         for (var i = 0; i < iRandom; ++i)
         {
             iIndex = Random.Range(0, AICarArray.Length); 
             iRandomDir = Random.Range(1, 3);
             
             //print(Vector3.Distance(transform.position, SpawnerArray[i].transform.position));
             
             if (!AICarArray[iIndex].active)
             {
                 AICarArray[iIndex].transform.position.x = SpawnerArray[i].transform.position.x; // Set car pos to spawner pos
                 AICarArray[iIndex].transform.position.y = SpawnerArray[i].transform.position.y + 1; 
                 AICarArray[iIndex].transform.position.z = SpawnerArray[i].transform.position.z; 
                 AICarArray[iIndex].transform.rotation = SpawnerArray[i].transform.rotation; 
                 
                 if (iRandomDir == 1)
                 {
                     AICarArray[iIndex].transform.tag = "AI_North";
                     AICarArray[iIndex].transform.localEulerAngles.y -= 180;
                     AICarArray[iIndex].transform.position.x += 2; // Put on right side of the road
                     //AICarArray[iIndex].rigidbody.velocity = transform.forward * -fInitialSpeedBoost; // Give initial boost
                 }
                 else
                 {
                     AICarArray[iIndex].transform.tag = "AI_South";
                     AICarArray[iIndex].transform.position.x += -2; // Put on left side of road
                     //AICarArray[iIndex].rigidbody.velocity = transform.forward * fInitialSpeedBoost; // Give initial boost
                 }
             
                 AICarArray[iIndex].SetActive(true);
                 bCarSpawned = true;
             }
         }
     }
 }
Comment
Add comment · Show 5
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 POLYGAMe · Sep 19, 2014 at 02:08 AM 0
Share

I don't know why the loop is crashing Unity but I've put the code inside Update and used a bool to trigger it, so it's working. Still be interested to know why it wasn't working, though.

avatar image rutter · Sep 19, 2014 at 02:11 AM 2
Share

Perhaps you're seeing an infinite loop? This seems like it could get messy if most or all of the AICarArray is already active.

avatar image Louis Watson · Sep 19, 2014 at 02:19 AM 0
Share

is this c#? if so your function declaration is wrong as you haven't specified a return type, consider void ins$$anonymous$$d.

avatar image smoggach · Sep 19, 2014 at 02:28 AM 1
Share

Only thing I can see is that maybe you got into that loop before you managed to set any AICarArray[iIndex].active

Try perfor$$anonymous$$g this in a Coroutine waiting a frame or two between iterations.

avatar image POLYGAMe · Sep 19, 2014 at 02:43 AM 0
Share

It's JS. Got it sorted. Cheers guys :)

1 Reply

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

Answer by Kiwasi · Sep 19, 2014 at 02:28 AM

Unity is not crashing. Unity is running like crazy carrying out your infinite loop. Line 34 will never trigger if all of the AICarArray items are already active.

A better solution would be to use a for loop to iterate through the entire collection looking for an inactive object. If none is found then a new one should be created or an error thrown.

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 POLYGAMe · Sep 19, 2014 at 02:43 AM 0
Share

Thanks. That's what I did. Cheers :)

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

28 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

Related Questions

wierd infinite while loop 1 Answer

Why does Unity crash ? 1 Answer

Why does while make Unity crash? 1 Answer

Unity 4 Build not working on iOS 5 1 Answer

Massive memory usage during compile time 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