Quantcast
Channel: Questions in topic: "crash"
Viewing all articles
Browse latest Browse all 2383

Why would this while loop crash Unity?

$
0
0
I have the following code. It runs fine when I don't put it inside a while loop, but I need to as I need to try spawning another car if the one attempted is active already. I can't see anywhere that this would cause an infinite loop... It's being called in OnTriggerEnter, not within update. function SpawnCar() { while (!bCarSpawned) { for (var i = 0; i < iRandom; ++i) { iIndex = Random.Range(0, AICarArray.Length); iRandomDir = Random.Range(1, 3); //print(Vector3.Distance(transform.position, SpawnerArray[i].transform.position)); if (!AICarArray[iIndex].active) { AICarArray[iIndex].transform.position.x = SpawnerArray[i].transform.position.x; // Set car pos to spawner pos AICarArray[iIndex].transform.position.y = SpawnerArray[i].transform.position.y + 1; AICarArray[iIndex].transform.position.z = SpawnerArray[i].transform.position.z; AICarArray[iIndex].transform.rotation = SpawnerArray[i].transform.rotation; if (iRandomDir == 1) { AICarArray[iIndex].transform.tag = "AI_North"; AICarArray[iIndex].transform.localEulerAngles.y -= 180; AICarArray[iIndex].transform.position.x += 2; // Put on right side of the road //AICarArray[iIndex].rigidbody.velocity = transform.forward * -fInitialSpeedBoost; // Give initial boost } else { AICarArray[iIndex].transform.tag = "AI_South"; AICarArray[iIndex].transform.position.x += -2; // Put on left side of road //AICarArray[iIndex].rigidbody.velocity = transform.forward * fInitialSpeedBoost; // Give initial boost } AICarArray[iIndex].SetActive(true); bCarSpawned = true; } } } }

Viewing all articles
Browse latest Browse all 2383

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>