- Home /
What's wrong with this code and is there an easier way to write it!?
Hi, all I want to do is instantiate a prefab I already have setup at a random spawn point. The spawn points are just Empty Game Objects and there are a few of them setup on my map. I have written this code:
#pragma strict
private var spawnNum = 0;
function Awake () {
spawnTesting();
}
function spawnTesting () {
WaitForSeconds(spawnNum * 50);
spawnNum = Random.Range(0, 1);
numCheck();
}
function numCheck() {
if(spawnNum <= 0.1) {
Debug.Log("Spawn1");
spawnTesting();
} else if (spawnNum <= 0.2) {
Debug.Log("Spawn2");
spawnTesting();
} else if (spawnNum <= 0.3) {
Debug.Log("Spawn3");
spawnTesting();
} else if (spawnNum <= 0.4) {
Debug.Log("Spawn4");
spawnTesting();
} else if (spawnNum <= 0.5) {
Debug.Log("Spawn5");
spawnTesting();
} else if (spawnNum <= 0.6) {
Debug.Log("Spawn6");
spawnTesting();
} else if (spawnNum <= 0.7) {
Debug.Log("Spawn7");
spawnTesting();
} else if (spawnNum <= 0.8) {
Debug.Log("Spawn8");
spawnTesting();
} else if (spawnNum <= 0.9) {
Debug.Log("Spawn9");
spawnTesting();
} else if (spawnNum <= 1) {
Debug.Log("Spawn10");
spawnTesting();
} else {
spawnTesting();
}
}
So all I am doing is when the game is started, Awake calls a function to start a cycle to check a Random.Range and then calls a debug.log which will eventually be an instantiate moving it to the specific spawn point.
I think I have made some sort of infinite loop because my Unity keeps 'Not Responding' when I try to test the game. I am new to coding and would like to know if the way I am checking for the Random numbers is efficient enough or could be shortened and also how I can or could re write the code so I don't get an infinite loop going.
Thanks!
When you're posting code, make sure you format it as such, otherwise it is just unreadable. Here's a prettier version:
private var spawnNum = 0;
function Awake() {
spawnTesting();
}
function spawnTesting() {
WaitForSeconds(spawnNum * 50);
spawnNum = Random.Range(0, 1);
numCheck();
}
function numCheck() {
if (spawnNum <= 0.1) {
Debug.Log("Spawn1");
spawnTesting();
} else if (spawnNum <= 0.2) {
Debug.Log("Spawn2");
spawnTesting();
} else if (spawnNum <= 0.3) {
Debug.Log("Spawn3");
spawnTesting();
} else if (spawnNum <= 0.4) {
Debug.Log("Spawn4");
spawnTesting();
} else if (spawnNum <= 0.5) {
Debug.Log("Spawn5");
spawnTesting();
} else if (spawnNum <= 0.6) {
Debug.Log("Spawn6");
spawnTesting();
} else if (spawnNum <= 0.7) {
Debug.Log("Spawn7");
spawnTesting();
} else if (spawnNum <= 0.8) {
Debug.Log("Spawn8");
spawnTesting();
} else if (spawnNum <= 0.9) {
Debug.Log("Spawn9");
spawnTesting();
} else if (spawnNum <= 1) {
Debug.Log("Spawn10");
spawnTesting();
} else {
spawnTesting();
}
}
Answer by rgowen · May 17, 2014 at 05:03 AM
The short answer is yes, you are creating an infinite loop. You have two functions that call each other, and every possible code path in either function will execute the other one. Thus, when you call one, it will inevitably call the other one creating an infinite loop. What you're trying to do actually requires an infinite loop, but there's a better way to do it.
You should be using a while
loop within your spawnTesting()
coroutine to ensure that it keeps running, not by calling it from another function. Also you can avoid all those if
statements by just concatenating the spawnNum
into your Debug.Log()
call.
private var spawnNum = 0;
function Awake() {
spawnTesting();
}
function spawnTesting() {
while(true) {
WaitForSeconds(spawnNum * 50);
spawnNum = Random.Range(0, 10);
numCheck();
}
}
function numCheck() {
Debug.Log("Spawn" + spawnNum);
}
If you have any questions feel free to ask.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Infinite Looping Crash 2 Answers
'brake' statement makes Unity hang ? 0 Answers
Im trying to make an Timer within a for loop, cant figure it out. 1 Answer