Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
This question was closed Aug 07, 2017 at 09:18 AM by Parksters for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by Parksters · Jul 28, 2017 at 11:49 PM · c#arrayobjectfor-loop2d array

Values in Array Automatically Revert

I am using a for-loop that changes a value of an object in an array of 2D objects, though after exiting the for-loop, the value of the object will revert to how it was before the loop. However, when I put the same type of object into the same for-loop, except where the object is not part of any array, it works as intended, and it's value is not reverted.

The code bellow contains several lines that are unnecessary and could be simplified that I'm aware of; however, I am only focoused on the problem mentioned above, and what exactly the cause of it may be.

    public class C_Generate_Level : MonoBehaviour {
 
     T[,] InitializeArray<T>(int height, int width) where T : new()
     {
         T[,] array = new T[height, width];
         for (int i = 0; i < height; ++i)
         {
             for (int g = 0; g < width; ++g)
             {
                 array[i, g] = new T();
             }
         }
         
         return array;
     }
     
     
     class tile 
     {
         public GameObject fabCurve_Tile = Resources.Load("Curve_Prefab") as GameObject;
         public GameObject fabTee_Tile = Resources.Load("Tee_Prefab") as GameObject;
 
         public void initialize_curve ()
         {
             entity = fabCurve_Tile;
             t_top = -1;
             t_right = 1;
             t_bottom = 1;
             t_left = -1;
             t_rotation = 0;
         }
         
         public void initialize_tee ()
         {
             entity = fabTee_Tile;
             t_top = -1;
             t_right = 1;
             t_bottom = 1;
             t_left = 1;
             t_rotation = 0;
         }
 
         public void initialize_null ()
         {
             entity = null;
             t_top = 0;
             t_right = 0;
             t_bottom = 0;
             t_left = 0;
             t_rotation = 0;
         }
 
         public GameObject get_entity ()
         {
             return entity;
         }
 
         public int get_rotation ()
         {
             return t_rotation;
         }
 
         public void nullify_rotation ()
         {
             t_rotation = 0;
         }
 
         public void TEST_ROTATE ()
         {
             t_rotation = 1;
         }
         
         GameObject entity;
         int t_top;
         int t_right;
         int t_bottom;
         int t_left;
         int t_rotation;
     };
     
 
 void Start ()
 {
     const int GRID_HEIGHT = 5;
     const int GRID_WIDTH = 5;
     const int REPEAT_LIMIT = 10;
         
     int heightCounter;
     int widthCounter;
     int tileRedoCounter;
 
     bool bStop;
 
         int do_while_counter;

         tile[,] tilesAry = InitializeArray<tile>(GRID_HEIGHT, GRID_WIDTH);
 
         tile dummyTile = new tile ();
         dummyTile.initialize_tee ();


     tile curveTile = new tile ();
     curveTile.initialize_curve ();
                         
     tile teeTile = new tile ();
     teeTile.initialize_tee ();  
                       
     tile nullTile = new tile ();
     nullTile.initialize_null ();

 
         heightCounter = 1;
         widthCounter = 1;
 
             tilesAry[heightCounter, widthCounter] = nullTile;

                 bStop = false;
                 for (tileRedoCounter = 0; tileRedoCounter <= REPEAT_LIMIT; tileRedoCounter++)
                 {
                     curveTile.initialize_curve ();
                     teeTile.initialize_tee ();
                     nullTile.initialize_null ();
 
                     tilesAry[heightCounter, widthCounter].nullify_rotation ();
                     if (true == bStop)
                     {
                         break;
                     }
                         tilesAry[heightCounter, widthCounter] = curveTile;


                         tilesAry[heightCounter, widthCounter].TEST_ROTATE();

                         bStop = true;


                     dummyTile.TEST_ROTATE ();
            // dummyTile and tilesAry[1, 1] both corectly have "1" for their t_rotation values here
                 }
            // but here, tilesAry[1, 1] has it's t_rotation value revert back to "0"
     }
 
 }
Comment
Add comment · Show 2
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 ShadyProductions · Aug 07, 2017 at 09:19 AM 0
Share

$$anonymous$$y eyes burn just by looking at the code

avatar image cgarossi ShadyProductions · Aug 07, 2017 at 09:22 AM 1
Share

I'm guessing he's come from Python. It looks very Pyhtonesque.

1 Reply

  • Sort: 
avatar image
0
Best Answer

Answer by cgarossi · Aug 07, 2017 at 08:30 AM

The only reason I can see why that would happen is

 tilesAry[heightCounter, widthCounter].nullify_rotation ();

Is being called, then your:

                  if (true == bStop)
                  {
                      break;
                  }

Is being executed. So you exit the loop with rotation of zero.

You are setting bStop to true, then returning to the top of the loop, which will nullify your rotation and exit.

Comment
Add comment · Show 4 · 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 Parksters · Aug 07, 2017 at 08:55 AM 0
Share

Thanks for the input, though that does not seem to be the solution; I removed the nullify_rotation line, and there is no difference.

avatar image cgarossi Parksters · Aug 07, 2017 at 09:07 AM 1
Share

You're not constructing a new instance of curve tile. Each item in the array is the same instance.

So when curetile.initialize_curve() is called, you are resetting everything in the array back to zero and it will break out of the loop before it gets set again, as bStop is true.

avatar image Parksters cgarossi · Aug 07, 2017 at 09:18 AM 0
Share

Thank you; removing curveTile.initialize_curve () solved the problem.

Show more comments

Follow this Question

Answers Answers and Comments

76 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 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 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 avatar image avatar image avatar image avatar image

Related Questions

Modifiying One Value in an Array Modifies All Values 1 Answer

Distribute terrain in zones 3 Answers

Error CS0029 Help? (Screenshot of Exact Error) 1 Answer

How to save a 2d-array in C# 2 Answers

How to deactivate all GameObject in a array, except last one 4 Answers


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