Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by coolbird22 · Sep 07, 2014 at 12:20 PM · arrayrandomlogic

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 ();
                 }
             }
         }
     }
 
Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Malfegor · Sep 07, 2014 at 12:37 PM 0
Share

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

avatar image coolbird22 · Sep 07, 2014 at 12:57 PM 0
Share

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.

avatar image coolbird22 · Sep 07, 2014 at 01:09 PM 0
Share

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();

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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;
                     }
                 }

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image coolbird22 · Sep 10, 2014 at 04:49 AM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

24 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges