Unity stop working after running this code, help!!!!!!!! (decremented of for varible)
unity stop work after i run this code: i used "i--;" before, but for some reason i don't work this time, pls help
public GameObject[] Cards = new GameObject[3];
public Transform[] Spawnpoint = new Transform[6];
bool[] TakenPos = new bool[6];
int Pos;
void Start () {
for (int i = 0; i <= 3; i++){
for (int j = 0; j <= 2; j++){
Pos = Random.Range (0,5);
if (TakenPos [Pos] == false) {
Debug.Log ("Spawnpoint: " + Pos);
TakenPos [Pos] = true;
} else {
Debug.Log ("Spawnpoint: OCUPADO");
j--;
}
}
}
}
Answer by Seabat10 · Aug 30, 2018 at 02:17 PM
i have solved this, i will show you the code: this throw random numbers without repeat ;) void RandomNumbers(){
num[0] = Random.Range (0, num.Length); //El primer numero no hace falta compararlo
for (int i = 1; i < num.Length; i++) { //Va tirar numeros aletorios dependiendo del array
num [i] = Random.Range (0, num.Length); //Numero aleatorio
check = false; //Resetea la variable
while(check == false){ //checkea el boolean
if (i != j) { //Si no se compara con sigo mismo
if (num [i] == num [j]) { //Si es igual a otro numero de array, tira otro numero
num [i] = Random.Range (0, num.Length);
j = 0; //Resetea el comparadro
} else { //Si es correcto, aumenta el comparador
j++;
}
} else { //Si se compara con sigo mismo, salta a la siguiente
j++;
}
if (j == num.Length) { //Si se comparo con todos, resetea variable, quiebra el while y pasa a la siguiente
check = true;
j = 0;
}
}
}
}
Answer by JVene · Aug 16, 2018 at 10:43 PM
If I assume that the TakenPos array starts out filled with false values (it is unless there is other code involved not posted).
This means that unless Pos never happens to generate the same value twice within about 5 tries (which is a very low probability), this would run forever. Put another way, all entries in TakenPos start out as false, which means any Pos value will satisfy the first test, setting the TakenPos[ Pos ] to true. However, there is a very good likelihood that Pos will end up being set to a value already set to true, at which point j is decremented. As the routine proceeds, however, the probability that Pos produces a value for which TakenPos[ Pos ] is already true approaches 1 (or 100%).
Put another way, it would be a very rare situation where this code doesn't create a TakenPos array that is all true, and thus defines an infinite loop. That is ensured by the outer loop in 'i', because even though j will be reset to zero, TakenPos will already be a list of 'true' bools, or so close to it that by the second or third pass through 'i', there's virtually no chance the first test will ever fire again, meaning this will eternally loop with j being decremented, then incremented, then decremented, then incremented - with no possible way out.
What you require is to decide what TakenPos should look like when correctly initialized. I assume that would be somewhere around 1/3 to no more than 2/3rds true entries, and the rest as false entries remaining.
A simple, single loop in j of 6 items would suffice, without decrementing j, and no outer loop in i, merely skipping any repeated Pos entries.
However, this is likely a waste of time. The theory of operation is that only Pos entry duplicates can hope to create non-true results in the result array of TakenPos.
It would be better and more straight forward to establish a volume that is randomly assigned between 2 and 4 (or some other extremes, I merely assume you'd want at least two true entries but no more than 4 entries). Then, loop that volume, creating the random Pos entries for that count, perhaps not even bothering to check if they are already true (that merely continues to randomize the ultimate volume of true entries).
Your answer
Follow this Question
Related Questions
How do i render a Mesh loop? 1 Answer
How Can I Reset Values?(Unity3D) 0 Answers
How to make the unity editor respond while a script is working? 2 Answers
Problem with decrement 0 Answers