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);
}
}
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;
}
Your answer
Follow this Question
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