- Home /
Trouble with 2D Array for board game
G'day community,
I'm working on a Turn-Based Strategy game in Unity, and am up to the 'playing field' itself. I've worked with a (pseudo) multidimensional array of integers before for a maze game, but this project calls for a multidimensional array of gameobjects.
I've got a 4x4 'board' of cubes, each with the same script attached, that changes their colour when the mouse moves over them.
For the 'unit', it has a script that is trying to access a multidimensional array of gameobjects (the 'board' cubes) in another script, using an (int) index for both columns and row, to access the GameObject in the next/previous column or row, and change the colour of it.
Here's what's on the 'unit'. It's supposed to change the colour
function OnMouseUp() {
var colIndex = 0;
var rowIndex = 0;
var targetGameObject : GameObject;
var someScript;
someScript = GetComponent ("fieldScript");
activeUnit = true;
targetGameObject = someScript.grid[rowIndex][colIndex+1];
}
When I try to run this, the last line returns this error: "Object reference not set to an instance of an object".
Now, in an empty game object I have the below code, which attempts to create a 2d array of gameobjects. Each game object is assigned to the rows using the property inspector.
var rowA : GameObject[];
var rowB : GameObject[];
var grid = [
[rowA],
[rowB]
];
I've worked for hours trying to solve this, but nothing seems to work. I'm posting in the hopes somebody might see the cause of my dilema, but please, I need specifics. I've read almost every post/thread to be found on everything remotely related to what I'm trying to achieve.
Cheers,
Answer by GuyTidhar · Jul 02, 2011 at 08:59 AM
Is "fieldScript" a script found on the same Game Object as this script is attached to? If not - it should, since you are looking for a component on the same game object.
Is "fieldScript" the exact name of that script? When possible don't look for strings. Instead use script types. If you named your script ""fieldScript", then GetComponent(fieldScript) should work.
Don't call getcomponent and find functions repeatedly. You should only use them for initialization (or drag the game objects to public variables when possible).
in your case:
var someScript;
function Start()
{
someScript = GetComponent ("fieldScript");
}
function OnMouseUp()
{
var colIndex = 0;
var rowIndex = 0;
var targetGameObject : GameObject;
activeUnit = true;
targetGameObject = someScript.grid[rowIndex][colIndex+1];
}
That script is attached to the 'unit'. The reason I used GetComponent is that to access it otherwise (fieldScript.grid etc.), I needed to make it a static var, which wouldn't work because the two arrays (rows) of gameObjects, once static, wouldn't appear in the property inspector to have the gameObjects inserted into them.
I'd be happy to send you my project file, if you're interested. This one is driving me mad.
You can copy the reference of non static public variables to your static variables and then it should work.
You are more then welcome to post a link to the project file (yousendit.com...) or something and I'll look into it as soon as I can (it'll take a few hours till I can though)
Cheers, it's here - http://www.mediafire.com/?fo1ex2mwt8ui8fg. Creating a static variable to mirror the non-static ones returns an error, 'Array index is out of range'. I'd greatly appreciate if you found time to have a look.
Here are the update scripts - I've changed the unityScript and fieldScript. https://www.yousendit.com/download/$$anonymous$$Fo3$$anonymous$$0d1ZDVC$$anonymous$$TZ4dnc9PQ
Sorry it has been so long. This works! Now that this basic interaction with other gameobjects is working, I can set about creating a better 'board', an proper multidimensional array, preferably. Thanks bloke, this has set me on the right track!
Answer by Eric5h5 · Jul 02, 2011 at 10:13 AM
It's far better if you just use an actual multidimensional array:
var board : GameObject[,];
function Start () {
board = new GameObject[4, 4];
}
How would I populate that array then? It's important which order the GameObjects appear.
for(var X : int = 0; X < board.GetLength(0); X++)
for(var Y : int = 0; Y < board.GetLength(1); Y++) {/* board[X,Y].doSomething(); */}
@Ozale: probably the easiest way would be to instantiate the gameobjects in the array from a prefab.
@Eric5h5: do you know of any resources/tutorials for this technique?
@Ozale: Use SilverTabby's code, plus
board[x,y] = Instantiate(prefab);
If you mean tutorials for instantiating, the docs cover it pretty thoroughly.
Answer by Waz · Jul 02, 2011 at 09:34 AM
This will not work:
var rowA : GameObject[];
var rowB : GameObject[];
var grid = [
[rowA],
[rowB]
];
At the time grid
is initialised, rowA and rowB are null. The only way I see this working is to set grid in Awake().
Your answer
Follow this Question
Related Questions
Passing a Multidimensional array in Javascript 3 Answers
multidimensional builtin vector3 array? 3 Answers
Visializing a multidimensional array 3 Answers
Arrays vs Coordinates 2 Answers
Multidimensional .net array problem 1 Answer