- Home /
 
Unable to change a value of an Integer inside a 2D array
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 ();
                 }
             }
         }
     }
 
 
              Allriiight, I'll rewrite a bit for ya and hope it works:
 bool HasNewGoal;
 
 public void randomizeNewGoal
 {
     HasNewGoal = false;
     
     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;}
                 }
             }
         }
 
     while(!HasNewGoal)
     {
         int randomGoalI = Random.Range(1,12);
         int randomGoalII = Random.Range(1,16);
 
         if(Get$$anonymous$$apItem(randomGoalI, randomGoalII) > 0) continue;
 
         mapArray[randomGoalI, randomGoalII] = 3;
 
         // Instantiate you stuff!
 
         HasNewGoal = true;
     }
 }
 
 private int Get$$anonymous$$apItem(int x, int y)
 {
     return mapArray[x, y];
 }
 
                  I hope this works, but I have my doubts the Instantiating will work as planned, please let it know if something's still not working/if you need any more help
Hi @$$anonymous$$alfegor. Thanks for co$$anonymous$$g to my rescue for the 2nd time in a day. I tried the code you suggested, and it is still acting the way it was, when I tried my code. All it seems to be doing is changing the 2 to 0 as well as randomizing a 0 to a new 3, but it never manages to change the old 3 to 2. Lastly, I appreciate all the help you provide. Thanks again.
Ok, I went back to being a stone age coder and broke everything up into respective methods -
1) reset 2 to 0.
2) reset 3 to 2.
3) randomize and create a new 3 from all available 0s.
Here is what I did:
 public void randomizeNewGoal ()
     {
 
         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 ();
         
         
     }
 
 
     public void resetOldStart()
     {
         for(int i = 0; i<13; i++)
         {
             for(int ii = 0; ii < 17; ii++)
             {
                 if ( mapArray[i,ii] == 2 ){
                     mapArray[i,ii] = 0;
                     
                     
                 }
             }
         }
     }
 
 
     public void resetGoal()
     {
         for(int i = 0; i<13; i++)
         {
             for(int ii = 0; ii < 17; ii++)
             {
         if ( mapArray[i,ii] == 3 ){
             mapArray[i,ii] = 2;}
             }
         }
     }
 
                  and then I called them in the chronologically required order.
 resetOldStart();
 resetGoal();
 randomizeNewGoal();
 
                 Answer by dsada · Sep 07, 2014 at 01:05 PM
You just placed the condition maparray[i,ii] == 3 inside the maparray[i,ii] == 2 brackets. So it is obvious if you are in the "== 2" the "== 3" will not be true in any case.
So instead of this:
 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;}
                 }
             }
 
               you should wirte this:
 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;
                 }
             }
 
               or its even better if u use switch or else if
 for(int ii = 0; ii < 17; ii++)
                 {
                     if ( mapArray[i,ii] == 2 ){
                         mapArray[i,ii] = 0;
                     }
                     else if ( mapArray[i,ii] == 3 ){
                          mapArray[i,ii] = 2;
                     }
                 }
 
              Although breaking up my code into smaller methods did work for me, this is surely a better way to code it, hence marking this as the correct answer. Thanks for chi$$anonymous$$g in @$$anonymous$$alfegor and @dsada.
Your answer
 
             Follow this Question
Related Questions
problem extracting transform from an array 1 Answer
How do you get different random numbers for each object array? 1 Answer
Randomize Audio Clip 1 Answer
Unique # Generation "Cannot cast from source type to destination type." 3 Answers
Spawn object using vector array anywhere but previous spawn location 1 Answer