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 Jul 20, 2017 at 04:53 AM by Parksters for the following reason:

I guess I solved this, somehow.

avatar image
0
Question by Parksters · Jul 19, 2017 at 11:04 PM · c#array of gameobjects2d array

Changing One Value in an Array Changes All Values

I've created a 2D array of object called "tile", who's member variables are

GameObject entity; int t_top; int t_right; int t_bottom; int t_left;

Whenever I assign a tile value to any element in the 2D array, every element in the array is changed to that same value. I tried to only include the most relevant snippets of code:

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

...

         public void initialize_straight ()
         {
             entity = fabStraight_Tile;
             t_top = 1;
             t_right = -1;
             t_bottom = 1;
             t_left = -1;
         }
 
         public void initialize_blank ()
         {
             entity = fabBlank_Tile;
             t_top = -1;
             t_right = -1;
             t_bottom = -1;
             t_left = -1;
         }
 
         public GameObject get_entity ()
         {
             return entity;
         }

...

     const int BUFFER = 2;
     const int GRID_HEIGHT = 2 + BUFFER;
     const int GRID_WIDTH = 2 + BUFFER;
 
     int heightCounter;
     int widthCounter;

...

         tile[,] tilesAry = InitializeArray<tile>(GRID_HEIGHT, GRID_WIDTH);

...

     for (heightCounter = 0; heightCounter < GRID_HEIGHT; heightCounter++)
     {
         for (widthCounter = 0; widthCounter < GRID_WIDTH; widthCounter++)
         {
             tilesAry[heightCounter, widthCounter].initialize_blank();
         }
     }

     for (heightCounter = 0; heightCounter < GRID_HEIGHT; heightCounter++)
     {
         for (widthCounter = 0; widthCounter < GRID_WIDTH; widthCounter++)
         {
             print (tilesAry [heightCounter, widthCounter].get_entity () + " yyy");
         }
     }
     print (" ");
     for (heightCounter = 0; heightCounter < GRID_HEIGHT; heightCounter++)
     {
         for (widthCounter = 0; widthCounter < GRID_WIDTH; widthCounter++)
         {
             tilesAry [heightCounter, widthCounter].initialize_straight();
         }
     }
     print (" ");
     for (heightCounter = 0; heightCounter < GRID_HEIGHT; heightCounter++)
     {
         for (widthCounter = 0; widthCounter < GRID_WIDTH; widthCounter++)
         {
             print (tilesAry [heightCounter, widthCounter].get_entity () + "yyy");
         }
     }

In the above snippet, the first time it prints out saying all of the tiles are blank (as it should), while the second time it says that all are straight (when only one of them should be.

However, for whatever reason, this second array (DUBB) that I made just for testing this problem works normally, and does not show any sign of this same issue:

     tile[,] DUBB = new tile [3, 3];
     DUBB[0,0] = new tile ();
     DUBB[1,1] = new tile ();
     DUBB[2,2] = new tile ();
     
     DUBB[0,0].initialize_blank ();
     DUBB[1,1].initialize_blank ();
     DUBB[2,2].initialize_blank ();
     
     print (DUBB[0,0].get_entity ());
     print (DUBB[1,1].get_entity ());
     print (DUBB[2,2].get_entity ());
     print ("Break");
     
     
     DUBB[1,1].initialize_straight();
     print (DUBB[0,0].get_entity ());
     print (DUBB[1,1].get_entity ());
     print (DUBB[2,2].get_entity ());


Is there any other code that I might need to add to better display what is happening?

Comment
Add comment · Show 6
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 Habitablaba · Jul 19, 2017 at 11:46 PM 0
Share

I'm not entirely sure what you are saying. You are doing two completely different things in your last two blocks. The first one, with the nested for loop, is going to every single place in the array and setting the value to nullTile. In the second one, you are accessing specific indices manually, and calling a method.

If you want the top version to be like the bottom version, then you need to add a bit of code to check the value of height counter vs that of width counter, and only assign the value if they are equal.

avatar image revolute · Jul 20, 2017 at 12:51 AM 0
Share

Provide initialize_null() , assign() functions. We can't know what goes in there. If "initialize_null()" uses shared variable as null, which I suspect you are doing, "tilesAry[heightCounter, widthCounter] = nullTile;", you would end up with same references over the array.

Now, if assign is something like "tile.x = inputTile.x" kind of value copying, then the last block will set "nullTile" to whatever "straightTile" is.

If you want such manner, I suggest you change to struct.

avatar image OneManEscapePlan · Jul 20, 2017 at 01:17 AM 0
Share

You haven't given us enough information. Is DUBB the second array that works? When do you assign values to the first array that isn't working? What does initialize_null() do?

avatar image bobisgod234 · Jul 20, 2017 at 01:59 AM 1
Share

while the second time it says that all are straight (when only one of them should be)

I may have missed something, but as far as I can tell, your code is iterating over every element and setting it as straight, so I would expect every element to become straight.

      for (heightCounter = 0; heightCounter < GRID_HEIGHT; heightCounter++) 
      {
          for (widthCounter = 0; widthCounter < GRID_WIDTH; widthCounter++)
          {
              // initialize_straight() is being called for every single element.
              tilesAry [heightCounter, widthCounter].initialize_straight();
          }
      }
avatar image Parksters bobisgod234 · Jul 20, 2017 at 04:54 AM 0
Share

Thanks; that wasn't the problem, but it led me to finding it, even though I still don't entirely understand it.

avatar image Dr_SuiuS · Jan 16, 2021 at 09:50 PM 0
Share

BU$$anonymous$$P

0 Replies

  • Sort: 

Follow this Question

Answers Answers and Comments

357 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 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 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 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 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

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Illuminating a 3D object's edges OnMouseOver (script in c#)? 1 Answer

How can I check if the collider is in my array and get which gameobject it is from the array? 1 Answer

Remove a gameObject from remote list (Spawner) 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