- Home /
What can i do? Unity no responding when i load a list.
Hello I don't understand what happened. I made a class with static property for manage the level and the sequence of enemy scene. The class is called globalData
I have another class called randomNumbers for calculate random number in a range. These numbers cannot be repeated. the method is called randomScene.
globalData has a private static method call loadingOrderScenes() for load a list.
When I comment the line where add new numbers to the list. The program run well. But when I put this line active. Unity no responding.
I think is because is cyclized.
What can i do?
public class globalData : MonoBehaviour {
//this property is for manage the sequence of enemy scene
public static List <int> orderScena;
// this property manage a level
public static int Level = 1;
// this property manage the characte for the user.
public static int personaje = 2;
public static void loadingOrderScenes()
{
RandomNumbers NumbersRandom;
NumbersRandom = new RandomNumbers();
int cantDatos = 0;
int number = 0;
orderScena = new List<int>();
orderScena.Add(personaje);
try {
Debug.Log("it going to enter " + orderScena.Count);
while (true)
{
number = NumbersRandom.randomScene(1,6);
if (!orderScena.Contains(number))
{
Debug.Log("it is on the While = " + cantDatos);
// HERE IS THE PROBLEM..................................
//orderScena.Add(number);
cantDatos ++;
}
if ( cantDatos == 7)
{
Debug.Log("it is on the while = " + cantDatos);
break;
}
}
orderScena.Add(7);
Debug.Log("is out: " + orderScena.Count);
}
catch (UnityException e)
{
Debug.LogError(e);
}
}
// this is the other method
public int randomScene(int min, int max)
{
int number = 0;
number = Random.Range(min , max);
return number;
}
Answer by richyrich · Oct 19, 2014 at 02:07 AM
A problem with your code is that cantDatos == 7, will never happen. The maximum value you send to the random.range is 6, but the documentation shows that the max will never be an output - "unless min equals max". "Returns a random integer number between min [inclusive] and max [exclusive]" http://docs.unity3d.com/ScriptReference/Random.Range.html
Therefore, cantDatos will reach 5 at highest.
A suggestion for starting out (as I am too) is that while(true) is a BAD idea. Always have an extra condition to prevent a lock up
E.g. int counter=0; while(true && counter<1000000) Then increment the counter at the end (inside the loop) counter++; This is still not great programming, but its an easy hack while experimenting
To make your code work without a hang, make the following changes... Comment out line 23. If you don't, cantDatos will only reach 6 during the loop. If you need to keep this, set cantDatos to 1 instead of 0. //orderScena.Add(personaje);
//number = NumbersRandom.randomScene(1,6);
//8 will not be included in the list, but 7 will
number = NumbersRandom.randomScene(1,8);
Incidentally, the reason why it only locks up when you have that extra line of code is because before then, your new value has not been entered into the List, therefore the if-statement continues to execute, even though it found the number before, and so cantDatos is still incremented and exits at the break on reaching 7.
Thanks for dedicate your time to answer. I changed my code and used these tips.
And it works !
good day
Your answer
Follow this Question
Related Questions
A node in a childnode? 1 Answer
null reference exception problem with generic lists in unityscript (i think...) 1 Answer
Screen record in unity3d 7 Answers
Is there a way to convert a 2d generic list to a string in unityscript 1 Answer
in PlayerNetworkMovement i wanna active a deathpanel but it is not taking gameobject 0 Answers