- Home /
C# Global Dynamic Arrays
Hi, I'm new to C# and general OOP, and I'm having some difficulty understanding the dynamic arrays.
I have two scripts, one called GridDeclare and one called GridSpawn. It's necessary that they're separate scripts.
Within GridDeclare I want the ability to adjust the grid size, so within my GridDeclare class I've used:
public int xsize = 10;
public int zsize = 10;
Which can obviously be modified within the Object Inspector.
Then I want to introduce my grids themselves.
public static int[,] grid;
public static int[,] run;
public static bool[,] runvalid;
However, I want them to be of size [xsize,zsize]. So within my Start() function I declared:
int[,] grid = new int[xsize, zsize];
int[,] run = new int[xsize, zsize];
bool[,] runvalid = new bool[xsize, zsize];
I then used Streamreader to read a tab delimited text file into my grid array.
I then analysed the data to fill in my run and runvalid tables.
Next I wrote a bunch of scripts which, upon enabling, will are meant to read in the run and runvalid arrays and edit them, and then save them again.
But obviously I've initialised them as just standard variables within my Start function, so I just get a NullReferenceException: Object Reference not set to an instance of an object.
So I'm left with a dilemma, in that I want to create a global array that I can adjust the size of at runtime, access from other scripts and save to, and the only way I can think of doing it right now is a really sloppy workaround where'd I'd save the array data to a text file every time I modify it and read it in every time I access it. Any tips?
Answer by rutter · Mar 22, 2012 at 02:59 AM
Looks like you may have declared two sets of variables named grid
, run
, and runvalid
.
Try changing your Start()
function from this:
int[,] grid = new int[xsize, zsize];
int[,] run = new int[xsize, zsize];
bool[,] runvalid = new bool[xsize, zsize];
To this:
grid = new int[xsize, zsize];
run = new int[xsize, zsize];
runvalid = new bool[xsize, zsize];
Otherwise, you're actually creating an entirely new set of variables which unfortunately happen to have the same name, type, and general behavior... but which also effectively stop existing at the end of that one code block.
(Wikipedia's article on variable scope has a little more explanation, if you're curious.)
You can't do that. For a start you'd need to declare it as e.g.:
int grid = new int[xsize,zsize];
But you can't, because xsize and zsize aren't static variables.
You can declare them within the function fine, but you can't declare them as public within the function, and they have to be public. xsize and zsize have to be static too. I can't see a way of doing this.
Are you responding to me? If so, please understand that declaration and assignment are not the same thing.
This compiles fine, for me:
public class Foo
{
static int[,] grid;
public int xsize = 5;
void Start ()
{
grid = new int[xsize,xsize];
}
}
Are you having a specific problem or error?
I just understood what you meant, and I love you <3.
I was being an idiot; I thought you meant do that from outside the Start function for some reason. But yes, you've fully solved my problem, thanks a lot.
Answer by DaveA · Mar 22, 2012 at 02:13 AM
Your answer may lie here: http://unity3d.com/support/documentation/ScriptReference/Array.html
Using this type of array, basically to resize it means copying it to another array with the new size (then back again I would presume).
Your answer
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
StartCoroutine in a public static void 1 Answer
GetComponent C# 1 Answer
Multiple Cars not working 1 Answer
Array index is out of range 2 Answers