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 luxiflex · Dec 16, 2016 at 02:26 PM · freezewhile loop

Unity keeps crashing in while loop

Hello, the next code creates a random position and then checks if the position isn't behind a wall or another object. If the position is behind another object, then it creates a new position untill it isn't behind a wall.

The code works how I want it to be, but unity keeps crashing after 30-60 seconds, even with a filter that always leaves the loop after 15 fails.

This is the code: using UnityEngine; using System.Collections;

 /// <summary>
 /// 1. Maak een empty game object en noem het SpawnManager.
 /// 2. Voeg het player object toe aan dit script.
 /// 3. Voeg iedere soort enemy toe die aan het begin van de game zichtbaar moet zijn.
 /// </summary>
 
 public class FishSpawner : MonoBehaviour{
 
     public float timer = 10;            // Time between every spawn
     public float maxRange = 75;            // Max range between player and spawnpoint
     public GameObject player;            // Player object to define the spawn positions
     public GameObject[] prefabs;        // Array with prefabs to spawn.
 
     private int layer = 1;                // Check in wich layer the player is.
     private IEnumerator coroutine;
 
     void Start()
     {
         // Start coroutine function
         coroutine = Spawn(timer);
         StartCoroutine(coroutine);
     }
 
 
     private IEnumerator Spawn(float waitTime) {
         while (true) {
             yield return new WaitForSeconds(waitTime);
 
             // Get player position
             float posX = player.transform.position.x;
             float posY = player.transform.position.y;
             float posZ = player.transform.position.z;
 
             // Check the layer where the player is
             if(posY > 399){
                 layer = 1;
                 maxRange = 30;
             }else if(posY > 299){
                 layer = 2;
                 maxRange = 60;
             }else if(posY > 199){
                 layer = 3;
                 maxRange = 100;
             }else if(posY > 99){
                 layer = 4;
                 maxRange = 125;
             }
 
             // Select a random object in the prefab array.
             int spawnPointIndex = Random.Range (0, layer -1);
 
             Vector3 spawnPosition = new Vector3(0,0,0);
 
             // New random spawn position
             spawnPosition = getPosition(posX, posY, posZ);
 
             // check for the while loop
             bool repeat = true;
             int exitCounter = 0;
 
             // Setting up the arraycast to check if there is an object between the player and the spawnpoint
             RaycastHit hit;
             Ray landingray = new Ray (player.transform.position, spawnPosition);
 
 
             while (repeat) {
                 // If exit counter equals 15, leave the while loop.
                 if (exitCounter == 15) {
                     repeat = false;
                 }
                 // unvalid position to spawn
                 if (Physics.Raycast (landingray, out hit, maxRange)) {
                     Debug.Log ("Recalculating spawn location.");
                     spawnPosition = getPosition(posX, posY, posZ);
                     landingray = new Ray (player.transform.position, spawnPosition);
                     exitCounter++;
                     repeat = true;
                 } 
                 // position is valid and object is spawned
                 else {
                     // object is klaar om gespawned te worden.
                     Debug.Log ("Object is ready to spawn.");
                     Instantiate (prefabs [spawnPointIndex], spawnPosition, new Quaternion (0, 0, 0, 0));
                     repeat = false;
                 }
             }
 
         }
     }
 
     Vector3 getPosition(float posX, float posY, float posZ){
         // create random position
         float randomX = Random.Range (posX - maxRange, posX + maxRange);
         float randomY = Random.Range (posY - maxRange, posY + maxRange);
         float randomZ = Random.Range (posZ - maxRange, posZ + maxRange);
         if (randomY > 360) {
             randomY = 360;
         }
 
         // Position van het nieuwe spawnpunt
         return new Vector3 (randomX, randomY, randomZ);
     }
 }
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Pengocat · Dec 16, 2016 at 03:04 PM

You never yield after entering the second while loop so that is probably why it gets stuck. Add a yield return null;

 while (repeat) 
 {
  // Do your stuff
  yield return null;
 }
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

85 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

Related Questions

[Solved]Cant Find loop that freezes Unity 2 Answers

Why does this while loop freeze unity? 1 Answer

While loop freeze 0 Answers

How can I simulate a thread? 1 Answer

Scene freezes when I restart level 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