Hi guys, I'm creating a script that actually instantiate my prefabs from an array at random position but in a special way (these positions are saved in another array too) .
Here is how my Gamelevel looks: http://postimg.org/image/fy8acipvn/
As you see they are forming a grid (4x4). In the first row I only want planes and the same with the rest of rows but every time the level starts they must appear at different position but always in the same row.
So what I did is create 3 arrays where at one I will save my positions, in another one my prefabs and the last is for save the positions that I already use for avoid overlapping.
In the prefabs's array I have all of them inserted in a way that from 0 to 3 (Planes), from 4 to 7(Cubes), from 8 to 11 (Rakes) and from 12 to 15 (Balls) and the same with the positions's array, from 0 to 3 (Planes pos), etc...
The problem in my code is when I am trying to detect if that position is already used.
I already spotted where the problem is but not which the problem is, is at the "IF (repetido == true)" (THE CODE IS BELOW) it's use if the random position generated is already in my array so generate a new one depending which prefab/image you are trying to instantiate to, so if the counter at prebafs's array is = 2, it must generate a new random number only from 0 to 3..
I don't know if this is the best way to do what I want do but is the only one I know for now, I am not any pro actually I am a beginner :D
When I uncomment that part and want start the game it goes to an endless loop but if I comment it I works well but I have overlapping.
Here the code where I have the problem, is a function called from another function:
public void respawner(){
while (repetido == false && continuar == false) {
for (j = 0; j < 16; j++) {
if ( randomPos == lastPos[j] ) {
repetido = true;
if (repetido == true) {
if( contador >= 0 && contador <= 3){
randomPos = Random.Range (0, 3);
}
if (contador >= 4 && contador <= 7) {
randomPos = Random.Range (4, 7);
}
if (contador >= 8 && contador <= 11) {
randomPos = Random.Range (8, 11);
}
else {//if (contador >= 12 && contador <= 15) {
randomPos = Random.Range (12, 15);
}
repetido = false;
j = -1;
}
}
if (j == 15) {
spawnImg (); //Function that instantiate my prefab
lastPos [contador] = randomPos;
continuar = true;
}
Bool "Repetido" is initialized at Start as false and the variable "Contador" is the counter of the array where I want save my positions already used. This counter goes parallel with prefabs's arrays cause if it values is 2 it means we are going to instantiate the image saved at position 2 and as I said before from 0 to 3 I have planes..
I don't know what I am doing wrong I'm just checking if that bool is true to generate another new position.. but Unity it seems doesn't like that IF.
↧