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