Quantcast
Channel: Latest Questions on Unity Answers
Viewing all articles
Browse latest Browse all 171066

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

$
0
0
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 !

Viewing all articles
Browse latest Browse all 171066

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>