- Home /
2d Array persists during multiple game sessions
I'm in the very early stages of building up a grid based rts game. For some reason the 2d array I'm creating (consisting of a custom object/script called GridCell) remains the same no matter how I change the values that determine the coordinates (sizeGridX and sizeGridY). The coordinates simply don't change after I've attached the script and ran the game once. Even if I multiply the sizeGridX by 10, it doesn't update unless I re-attach the script.
Is there any way that I can create a new array each time I start the game?
public var totalGridX : int = 10;
public var totalGridY : int = 10;
public var sizeGridX : int = 100;
public var sizeGridY : int = 100;
var tilePrefab : Transform;
var gridArray : GridCell[,] = new GridCell[10,10];
var mainCamera : GameObject;
//var nameN : int = 0;
var gridPlane : Transform;
function Start() {
// gridArray = GridCell[totalGridX, totalGridY];
for(var x : int = 0; x < totalGridX; x++){
for( var y : int = 0; y < totalGridY; y++){
var gc : GridCell = GridCell();
gc.CordX = (sizeGridX / 2) + (sizeGridX * x) ;
gc.CordY = (sizeGridY / 2) + (sizeGridY * y) ;
print(gc.CordX);
print(gc.CordY);
gridArray[x,y] = gc;
}
}
displayGrid();
}
function Update() {
}
function displayGrid() {
Debug.Log("test");
for(var x : int = 0; x < totalGridX; x++){
for( var y : int = 0; y < totalGridY; y++){
var gc : GridCell = gridArray[x,y];
print(gc.CordX);
print(gc.CordY);
Instantiate (gridPlane, Vector3(gc.CordX, 0, gc.CordY), Quaternion.Euler(-90, 0, 0));
print("Made it!");
}
}
}
Answer by Fattie · Jul 19, 2012 at 01:35 PM
Just add
@System.NonSerialized
on the line immediately before var gridArray. It's that easy.
Or! click the very little-known RESET button here:
I only learned this here on this here mailing list, so, now I am happy to tell someone else! Good luck
Thanks a ton :) Now ins$$anonymous$$d of having this unsolvable problem, I end up with three different solutions!
Done, I still have one question though. As I wrote as a comment to Seth's answer: when I change the gridArray to
gridArray = new GridCell[totalGridX,totalGridY];
I still get a warning that: "You are trying to create a $$anonymous$$onoBehaviour using the 'new' keyword. This is not allowed."
Is there anyway around this?
(1) I would EXTRE$$anonymous$$ELY STRONGLY URGE YOU to NOT use plain arrays and in fact you should use List. there are only very very rare specific situations, today, where you should ever use plain arrays. Have faith and use List.
(2) regarding your specific question, does this help http://answers.unity3d.com/questions/54695/how-to-declare-and-initialize-multidimensional-arr.html
As you know, this works fine ...
var gridArray : int[,];
then...
gridArray = new int[5,5];
so there must be something I don't understand about GridCell. Hope it helps!
Answer by Seth-Bergman · Jul 19, 2012 at 10:19 AM
Once you've attached the script, all subsequent changes to the variables must be made in the INSPECTOR panel, changing it in the script won't do anything. select the object in the scene containing the script, and look for the script in the inspector panel
EDIT:
Oh, and maybe add in the top of the Start function:
gridArray = new GridCell[totalGridX,totalGridY];
Thanks so much, I'd been looking for hours and figured that I created the array incorrectly. That definitely solves it though :)
One more question thoughwhen I change the gridArray as per your suggestion (and like I used to have it), I still get a warning that: "You are trying to create a $$anonymous$$onoBehaviour using the 'new' keyword. This is not allowed."
Is there anyway around this?