Why this script crashes Unity
Hi, I've been working on a spawning script to end up spawning enemies/ asteroids with the intention of making them spawn faster (so after 10 seconds there's a spawn, then after 9 seconds another spawn and 8 etc.) but I haven't been able to get the code to work and now I can't find errors in the script but it's crashing Unity when ran. I think it could be something to do with the loop or time algorithms.
Any help on this would be greatly appreciated, thanks!
Also any of the comments in the code are mainly just for testing purposes.
You should post code using code tags ins$$anonymous$$d of posting an image.
the goto statement is what makes Unity crash since you are making an infinite loop. Like Landern mentions you should not use the goto statement.
$$anonymous$$aybe you can use this as inspiration to do what you want?
using System.Collections;
using UnityEngine;
public class AsteroidSpawner : $$anonymous$$onoBehaviour
{
public GameObject largeAsteroid;
public float initialSpawnTime = 10.0f;
public float $$anonymous$$imumSpawnTime = 1.0f;
public float spawnTimeDecrement = 1.0f;
private float spawnTime;
private Vector3 asteroidPosition;
IEnumerator Start()
{
// Set initial spawntime
spawnTime = initialSpawnTime + spawnTimeDecrement;
// Wait 7 seconds before entering loop
yield return new WaitForSeconds(7f);
// Spawn the first Asteroid
SpawnAsteroid();
// Enter Infinite loop
while (true)
{
var countdown = Time.time + spawnTime;
while (countdown > Time.time)
{
// Do nothing if Countdown is not finished
yield return null;
}
SpawnAsteroid();
// Update countdown timer according to spawntime
countdown = Time.time + spawnTime;
}
}
void SpawnAsteroid()
{
float asteroidX;
float asteroidY;
if (RandomBool())
{
asteroidX = Random.Range(-11f, 11f);
asteroidY = RandomBool() ? 5f : -5f;
}
else
{
asteroidY = Random.Range(-5.5f, 5.5f);
asteroidX = RandomBool() ? 10f : -10f;
}
asteroidPosition = new Vector3(asteroidX, asteroidY, -1f);
Instantiate(largeAsteroid, asteroidPosition, Quaternion.identity);
DecreaseSpawnTime();
}
/// <summary>
/// Decrease the spawntime by spawnTimeDecrement if more than $$anonymous$$imumSpawnTime
/// </summary>
void DecreaseSpawnTime()
{
// Do not decrease if spawnTime is less than $$anonymous$$imumSpawnTime;
if (spawnTime < $$anonymous$$imumSpawnTime)
{
spawnTime = $$anonymous$$imumSpawnTime;
}
else if (spawnTime > $$anonymous$$imumSpawnTime)
{
spawnTime -= spawnTimeDecrement;
}
}
/// <summary>
/// Returns a random Bool
/// </summary>
/// <returns></returns>
bool RandomBool()
{
var random = Random.value > 0.5f ? true : false;
return random;
}
}
Thank you so much, I used IEnumerator and it works perfectly!
Answer by Landern · Dec 14, 2016 at 02:46 PM
Don't use goto statements. Use while loops with a breaking condition, do use InvokeRepeating and Coroutines to manage repeated events with timers, your goto statement probably has untended affects we can't account for in this small script, but the implementation will and is leading to poor results.
I do realize you have a constraint on execution using the timer greater than timerMax if statement, but this can be updated to use coroutines instead. No other issues seem to pop out from looking at your script. Though on line 93 you will probably want to adjust to use Quaternion.identity instead of a parameterless constructor which will indicate no rotation.
Instantiate(LargeAsteroid, AsteroidPos, Quaternion.identity);
Your answer
Follow this Question
Related Questions
Unity Crashing when time slows 1 Answer
Unity Editor constantly crashing 0 Answers
Fatal signal 11 (SIGSEGV), code 1, fault addr 0x50 0 Answers
Application.TickGlobalCallbacks causing crashing! 0 Answers
Unity just started crashing. 0 Answers