Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 ComradeVanti · Sep 25, 2015 at 05:59 PM · scripting problemloopfreezelooping

Unity freezes. I dont know why...

Hello community :D

I have a really big problem here since it totally prevents me from working on my game...

Here it is:

I have a game that spawnes enemys when i press Enter. All the scripts used for this are these:

The GameManager controlls how many rounds have passed and checks if i press Enter:

 public static int rounds = 1;
 public static bool roundIsRunning = false;

 void Update () {
     if (roundIsRunning == false && Input.GetKeyDown (KeyCode.Return)) {
         Debug.Log("Round startet");
         Debug.Log("This is round " + rounds);
         rounds++;
         roundIsRunning = true;
         EnemySpawner.activeSpawner.spawnEnemy(rounds);
     }
 }

The EnemysSpawner spawns all the enemys when called:

 private bool spawnNext = true;
 public Transform defSpawn;
 public GameObject enemyPrefab;
 public float maxOffset;
 public float spawnRate;
 public int remaining = 0;
 public int enemysOnField = 0;
 public static EnemySpawner activeSpawner;

 void Awake() {
     //Creating a static instance to be called form other scripts
     if (activeSpawner == null)
         activeSpawner = this;
     else if (activeSpawner != this)
         Destroy (gameObject);
 }

 public void spawnEnemy(int rounds){
     //Loop variable to Debug.Log how many loops have passed
     int loop = 0;
     Debug.Log("Enemys spawning...");
     //Remaining is the count of enemys based on the round you are in
     remaining = (int)Mathf.Log (rounds, 2f);
     Debug.Log ("Total enemys: " + remaining);
     //Spawning all the enemys in this loop
     while (remaining > 0) {
         loop++;
         if (spawnNext == true) {
             Instantiate (enemyPrefab, getSpawnPos(), Quaternion.identity);
             remaining--;
             Debug.Log("Spawned an enemy. Remaining:" + remaining);
             enemysOnField++;
             spawnNext = false;
             //Starting a coroutine so that there is a little time in between spawns
             StartCoroutine(waitForNextEnemy());
         }
         Debug.Log("Looped " + loop + " times");
     }
     Debug.Log ("All enemys spawned");
     GameManager.roundIsRunning = false;
     Debug.Log ("Round is over");
 }

 public Vector3 getSpawnPos(){
     Vector3 spawnPos = defSpawn.position;
     spawnPos.y += Random.Range (-maxOffset, maxOffset);
     return spawnPos;
 }

 IEnumerator waitForNextEnemy() {
     Debug.Log ("Waiting till next spawn...");
     yield return new WaitForSeconds (0.5f);
     spawnNext = true;
 }

I beg you to read through this and tell me what is wrong. I have already had some problems with coroutines but i dont know how this could be an issue here...

Also I should mention that it only freezes on the 3rd time I press Enter. The first 2 times it works perfectly fine.

In addition it freezes while still in the GameManager script. I can tell because it wont even show the first Debug.Log message. It freezes before that :/

I hope you guys can help me because this is starting to piss me off :(

Thanks in advance :D

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
1

Answer by Dave-Carlile · Sep 25, 2015 at 06:03 PM

This code (with the non-important parts stripped out) is your problem...

  while (remaining > 0) {
      loop++;
      if (spawnNext == true) {
          remaining--;
          spawnNext = false;
      }
  }

If remaining is > 0 (or 1) this will cause an infinite loop. Either spawnNext is false, in which case remaining never gets decremented, or spawnNext is true and it gets decremented once, after which spawnNext is changed to false and the loop will never end because remaining will never again be decremented.

Unity doesn't deal with infinite loops since everything runs on the same thread, so it hangs.

It looks like you want to spawn remaining enemies with a pause between each. It would probably work best to make this entire thing a Coroutine. In your Update, change the call to this...

 StartCoroutine(EnemySpawner.activeSpawner.spawnEnemy(rounds));

And change the header and loop in your spawnEnemy function...

 public IEnumerator spawnEnemy(int rounds)
 {
   ...

   for (int i = 0; i < remaining; i++)
   {
     ... instantiate the enemy and do your other stuff
     yield return WaitForSeconds(0.5f);
   }

   ...
 }
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 hexagonius · Sep 25, 2015 at 06:15 PM

your while is causing an infinite loop as soon as remaining gets bigger than 1. it only gets the chance to reduce by one once, then it's stuck waiting for spawnNext to get true which it never does because that's happening in the coroutine which is not getting updated anymore because of the whole being stuck.

as a good rule of thumb use while only ever in a coroutine with at least one yield. this will prevent it from hanging the execution. in your case, you could move it into the coroutine and run your decrease, spawn, wait, decrease, spawn, wait... in there.

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 ComradeVanti · Sep 25, 2015 at 07:14 PM

Alright. Thanks people. I will change the code right away. Glad i can continue my game now :D Thanks alot :D

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

31 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

Related Questions

Unity freezes when script is run in play mode 2 Answers

How do I wait for input in a for loop. 0 Answers

How to stop infinite loop in Unity? 1 Answer

Can't think of a way to do this... 0 Answers

Unity freezes when slider code is enabled. 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