- Home /
Crash on play befor the game even starts.
Im making a randomly generated game that has a number of pieces and then place there own pieces, that place there own pieces.... and so on till it reaches a limited number of pieces. When I press the play button unity crashes. the screen gets dim, and the play button turns grey (indicating that it is trying to start, usually thers is a little lag), but it never continues to change to the blue color (indicating that the game is now running). Here is my code.`var left : GameObject; var right : GameObject; var canspawn = true;
function Start () { print("started"); if(canspawn){ spawn(); } } function Update () {
}
function spawn(){ if(canspawn){ var thing : GameObject; var pice = Random.Range (1, 4); print (pice);
if(pice ==1){
thing = forward;
}
if(pice == 2){
thing = left;
}
if(pice == 3){
thing = right;
}
var nextpiece = Instantiate (thing, transform.position, transform.rotation);
nextpiece.BroadcastMessage ("cantnorth");
} }
function cantnorth(){ canspawn = true; spawn(); }
function cantsouth(){ //canspawn = true; //spawn(); }
function canteast(){ canspawn = true; spawn(); }
function cantwest(){ canspawn = true; spawn(); } `
I know from testing that where everything breaks is in the Spawn() function. When i comment it out, that whole thing runs fine. If anyone see's anything i did wrong, or anything I could do better, please tell me. Also if you have any knowlege of why crashes even happen in unity, I would love the help. Thanks a lot!
I'm not 100% I'm reading this right, but are these "forward", "left" and "right" objects using the same script? If so, the spawn script is infinitely recursive. By instantiating another "thing" in the spawn script, you're calling the new instance's Start function, which then calls the Spawn function, which then instantiates another object that does the same. It's essentially an infinite loop. Infinite loops will always crash unity
$$anonymous$$aybe, but I have had older versions of this code that went on infinantly. So I'm not totally sure it is that problem. I'll try that tho and see if I can come up with anything that will stop it from looping infinitely. Thanks for the input!
If you wanted it to loop infinintly you can ad yield WaitForSeconds (0.01) I find this will make unity run as fast as it can without crashing on start-up, I used this to make a fast rotating stick. Sorry if thats not what your trying to do.
Answer by MickM · Feb 27, 2013 at 01:37 PM
As in the comments, you are spawning an infinite amount of objects, this means that you will always run out of memory eventually which is why it is hanging. Currently there is no time that can spawn will ever be false (and you are never calling any function apart from cant north either)
Even if you add the delay as suggested, it will eventually still crash because you will keep spawning new objects forever (ie. until you run out of memory which will kill it!)
Your answer
Follow this Question
Related Questions
Android game shuts down but doesn't crash on startup 0 Answers
Unity Editor has stopped working" in Windows 8 0 Answers
Unity Editor Crashes Upon Startup 0 Answers
Android Game crashing when collecting collectibles 0 Answers
Unable To Understand Android Crash : android.view.ViewGroup.offsetRectBetweenParentAndChild 0 Answers