- Home /
Unity random generator kills programm
Hey everyone, first of all i am a complete Unity noob so please excuse any dumb questions: I am trying to make a 3D Jump 'n Run in Unity in which you have to complete a random Stage playing as a Ball. The Stage consists of 3 lanes, each 10 Units apart, to which the Ball can Jump to. Right now i am trying to make the randomizer which creates the Stage (The "Stage" consists of random Platforms scattered across the 3 lanes).
#pragma strict
var zPos = 7.5;
var xPos = 0;
var xHelp;
var ReMax = 15;
var Re = 0;
var Ground : GameObject;
function Start () {
Ground = GameObject.Find("Ground");
while (Re< ReMax){
if (xPos == 0){
xHelp= Random.Range(1,4);
Debug.Log (Random.Range);
if (xHelp== 1){
xPos = -10;
transform.Translate(xPos,0,0);
Instantiate(Ground);
transform.Translate(-xPos,0,0);
Re += Re;
transform.Translate(0,0,zPos);
}
if (xHelp== 2){
xPos = 10;
transform.Translate(xPos,0,0);
Instantiate(Ground);
transform.Translate(-xPos,0,0);
Re+= Re;
transform.Translate(0,0,zPos);
}
if (xHelp== 3){
xPos = 0;
transform.Translate(xPos,0,0);
Instantiate(Ground);
transform.Translate(-xPos,0,0);
Re += Re;
transform.Translate(0,0,zPos);
}
}
}
}
Unity itself doesnt give me an Errormessage when i try to run the Script but the program just frezzes as soon as i hit play. The Gameobject "Ground" are the Platforms the program is supposed to create. I have never worked with Instantiate so i kinda suspect my error to be the spawning of the Platforms. I've been stuck here for a while and dont realy know how to go on, so if someone would have a suggestion, i'd be very grateful.
Answer by Commoble · Mar 16, 2017 at 09:12 PM
You have an infinite loop in your Start function, which is why your game is freezing. Your loop is infinite because Random.Range(1,3) will only ever return the integers 1 or 2. When you use Random.Range() with integers, the range of possibilities start from the first number and exclude the last number. The way your loop is set up, it will never end.
Right, thanks for your reply first of all, so i just adjusted the numbers to Random.Range(1,4) and also changed the numbers of xHelp to 1,2 and 3 because i dont know what i was thinking setting them to 0,1 and 3, but that still did not change my problem of the Game freezing.
You've still got an infinite loop : as soon as xPos != 0, nothing is ever going to increment Re. And what on earth are all those transform.Translates meant to achieve? They'll just move the object to which this script is attached - is that what you want?
Since you are a new developer, I suggest that you start ASAP using some coding conventions. The C based conventions are pretty solid, but since you are using JS, I'm sure JS has some decent standard conventions. Just make your code easier to read for you, and everyone else.
Also, While loops are infinite loops until you break the while parameter. So your issue being Re always equals less than Re$$anonymous$$ax.
Answer by jjcrawley · Mar 17, 2017 at 12:30 AM
Can confirm, infinite loop. This issue can happen when you least expect, may be good practice to stick in a max number of iteration cycles. May help to prevent the editor from locking up. Or Unity could provide a quick stop feature in case this situation happens, that doesn't involve restarting the editor.
The loop is going to keep executing until the condition evaluates to false. Your condition is:
Re < ReMax
Re is 0, therefore doing Re += Re gives you zero, as 0 + 0 = 0. Therefore, Re is never increasing, as a consequence it will never be larger than ReMax, hence the infinite loop. If you are setting Re in the editor then make sure that it isn't 0, or less than 0.
One other thing worth mentioning, if xPos is not 0, then Re is never increased. If Re is never increased then it will never exit the loop. Why not change over to a for loop? May be safer, and you can specify a maximum number of tiles to place. Same principle, you'll just be iterating until you've placed the maximum number of tiles, rather than managing the counter manually in a while loop.
for(int iteration = 0; iteration < maxNumberTiles; iteration++)
{
//Place your tiles
}
That should remove the editor freezing issue.
Hopefully this helps.
Your answer
Follow this Question
Related Questions
noobHell, prefab arrays, GameObjects, Random Spawning 1 Answer
Game random freeze / hang up / stuck 0 Answers
Stuck on splash screen on Android only 0 Answers
Player gets stuck after AddForce in Air 3 Answers
Game freezes, cannot see why 1 Answer