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 Rnst1 · Jan 03, 2014 at 08:29 PM · variablesclass2d array

C# 2D Array of Class : why Class variables can't be changed?

Hy Everyone,

I get mad about a problem I have. I've created a public Class called "BlockIDClass" with several variables.

 using UnityEngine;
 using System.Collections;
 [System.Serializable]    
 
 public class BlockIDClass {
 
     public enum Type {None, Path, Dirt}
     public Type type            = Type.None;
 }

In another script, I've made a 2 dimensional array filled with BlockIDClass type object. I used a "for loop" to add the objects and each time I do this, I randomise an enumeration contained in the "BlockIDClass", so they are not the same in the array.

 using UnityEngine;
 using System.Collections;
 
 public class GridGenerator : MonoBehaviour {
 
     public int colNumber        = 10;
     public int rowNumber        = 10;
     public BlockIDClass [,] gridBlocksID    = new BlockIDClass [10,10];
     public BlockIDClass    gridBlockID;
 
     void Update ()
     {
         if (Input.GetKeyDown(KeyCode.Space))
         {
             generateGrid ();
         }
     }
     
     void generateGrid ()
     {
         for (int y=0; y < rowNumber; y++)
         {
             for (int x=0; x < colNumber; x++)
             {
                 int randomTypeNumber     = Random.Range(0, 3);
                 gridBlockID.type        = (BlockIDClass.Type)randomTypeNumber;
                 gridBlocksID [x,y]         = gridBlockID;
                 Debug.Log( "Enumeration type in for loop : "+gridBlocksID [x,y].type );
             }
         }
         foreach ( BlockIDClass i in gridBlocksID )
             {
               Debug.Log( "Final Enumeration type : "+i.type );
             }
         }
 }

When I Debug.log the enumeration type of each class object within the "for loop", it appears that they indeed have a different type :

  • Enumeration type in for loop : None 36

  • Enumeration type in for loop : Path 35

  • Enumeration type in for loop : Dirt 29

But when I check after the for loop, all the enumeration type of each class object is the same :

  • Final Enumeration type : Dirt 100

If you know what is happening, it will be a great help, thanks !

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 Rnst1 · Jan 04, 2014 at 11:50 AM 0
Share

Come on, anyone ? I will try to replace the 2D array of class by a 2D array of something else. I'll tell you if this works.

avatar image Rnst1 · Jan 04, 2014 at 05:37 PM 0
Share

$$anonymous$$mm, that may be that! I'll do a check with this solution and give a feedback. Thanks Phles!

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Phles · Jan 04, 2014 at 05:28 PM

Hey there, I think the problem is that you are setting variables on the same object, in your for loop where you have gridBlockID it is referencing the same object, so your array is just storing multiple references all to the same object. If you add gridBlockID = new BlockIDClass(); to the begining of your for loop it will be creating a new instance every iteration and putting this new object on the array that should solve your problem.

Comment
Add comment · 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
0

Answer by Rnst1 · Jan 04, 2014 at 06:04 PM

Ok, This works actually, this looks like this :

 for (int y=0; y < rowNumber; y++)
            {
              for (int x=0; x < colNumber; x++)
              {
                  BlockID gridBlockID     = new BlockID();
             int randomTypeNumber    = Random.Range(1, 3);
             gridBlockID.blocktype   = (BlockID.BlockType)randomTypeNumber;
                 gridBlocksID [x,y]      = gridBlockID;
              }
           }

I Just removed the gridBlockID declaration and put it in the for loop. Now each time that loops, a new instance of gridBlockID is created and put in the array. As usual, it was just a little thing. ;) Thanks for the help, Phles.

Dont forget to turn your comment into an answer to let me vote it up.

Comment
Add comment · 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

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

19 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

Related Questions

How to properly change variables of a sub class 0 Answers

Magic Spell Types - SpellType.Fire/Water/Earth? 1 Answer

How to get a List of all Variables in a Class 1 Answer

inheritance - instance variable problem? 4 Answers

Showing public class variables in another class in the Inspector. 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