I have a 2D array called ‘maparray’ which contains integers. When a player gameobject touches a particular goal gameobject, the goal calls a function called ‘randomizeNewGoal’ which has to randomize a new position for the goal (and set the current position of the goal as the new start position for player) inside the ‘maparray’ by assigning a value of 3 to a random position in the maparray with the value of 0, and also changing the previous 3 to 2***(new start position for player, as player always starts at 2)*** and the previous 2 to 0 (neutral)
I’m able to randomize the setting of the new goal from 0 to 3 as well as setting the old player start position from 2 to 0. But, the only thing I’m not able to accomplish is to set the old goal position to the new player position from 3 to 2. It might be a simple ‘if’ logic problem that I’m not able to get to work correctly.
The below is the code that I’m using:
public int[,] mapArray = new int[,]{
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1},
{1,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}
};
public void randomizeNewGoal () {
for(int i = 0; i<13; i++)
{
for(int ii = 0; ii < 17; ii++)
{
if ( mapArray[i,ii] == 2 ){
mapArray[i,ii] = 0;
if ( mapArray[i,ii] == 3 ){
mapArray[i,ii] = 2;}
int randomGoalI = Random.Range(1,12);
int randomGoalII = Random.Range(1,16);
if (mapArray [randomGoalI, randomGoalII] <1)
{
mapArray [randomGoalI, randomGoalII] = 3;
Instantiate (goal, new Vector3 (randomGoalI * offset, 0, randomGoalII * offset), Quaternion.Euler (90, 0, 0));
Instantiate (walkable, new Vector3 (randomGoalI * offset, 0, randomGoalII * offset), Quaternion.Euler (90, 0, 0));
}
else
randomizeNewGoal ();
}
}
}
}