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
-1
Question by Mysta · Feb 11, 2014 at 06:24 AM · instantiateprefabfindgameobjectswithtagwhile

While loop freezing Unity

So maybe I'm missing something extremely nooby, but I can't figure out why this is freezing the client.. the idea is to spawn up to 20, and each time one is destroyed, spawn a new one.

 public class spawnNew : MonoBehaviour {
     public int CoinsCount = 20;
     public GameObject Coins;
     public float spawnArea = 20.0F;
     
     void Update () {
         GameObject[] countObj;
         countObj = GameObject.FindGameObjectsWithTag ("Collect");
         print (countObj.Length);
         while  (countObj.Length < CoinsCount) {
 
             Vector3 spawnCoord = new Vector3(Random.Range(-spawnArea, spawnArea), 0, Random.Range(-spawnArea, spawnArea));
             Instantiate(Coins, spawnCoord, Quaternion.identity);
         }
     }
 }

Edit: Thank you for all the answers, I cannot upvote everyone as I'm still rather new but I appreciate it! Sometimes it's easy to get stuck in two different 'loops' and forget the core one isn't being updated =)

I switched from a while i< and had i++ at the bottom and forgot to update the countObj!

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

3 Replies

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

Answer by Josh707 · Feb 11, 2014 at 06:31 AM

If a loop is causing Unity to freeze it means the loop is infinite, or never reaches it's target.

You're checking if the length of the countObj array is less than CoinsCount, but the length of the array never changes after you first populate it, so it is less than CoinsCount forever.

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 Hoeloe · Feb 11, 2014 at 07:10 AM 0
Share

This is exactly the problem. It looks to me like you want to do one action for every element in the array. The for loop was designed for this purpose, or, better still, the foreach loop was designed specifically for iterating over arrays.

Loops work by testing a condition, doing the action, and then starting again. If you never change the outcome of the condition, then your loop will never finish.

avatar image Mysta · Feb 11, 2014 at 12:44 PM 0
Share

I definitely realized it was infinite, but could't see how(was probably getting too late) Thanks! =)

avatar image
1

Answer by whydoidoit · Feb 11, 2014 at 06:29 AM

So you aren't updating your countObj every loop (though that really isn't a very good way of doing this as it's a bit over the top!)

This would be better:

    var number = countObj.Length;
    while(number++ < CoinsCount) {
         ...
    }
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 Mysta · Feb 11, 2014 at 12:44 PM 0
Share

Thanks! Very simple fix

avatar image
-4

Answer by alti · Jan 15, 2020 at 07:24 AM

you can't do a while loop in update. Throw a:

yield return null;

somewhere in there, outside of any if statements and you'll be fine. I do not even know how the compiler allowed you to freeze things. While loops are done under IENumerators, not voids.

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 Hoeloe · Jan 15, 2020 at 07:49 AM 2
Share

This is completely incorrect information. You can absolutely use a while loop in update, you can use one anywhere. What you can't do is use a while loop that never ter$$anonymous$$ates without having some way for code execution to leave that loop, otherwise it will just happily continue doing calculations forever and freeze your program.

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

22 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

Related Questions

Instantiate new object from scratch 1 Answer

Spawning a prefab at another object's location 3 Answers

Instantiate individual components of a prefab? 1 Answer

Issue Instantiating prefab in C# 0 Answers

How do I make a clone of a prefab appear on the correct layer? [5.2.2f1] 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