Getting a variable from a GameObject inside a 2d array?

Hi there!

I’m pretty sure that I’m doing this wrong, but thanks for taking a moment to look at my question:

I have a 2d array of game objects which each have a script on them that is storing 2 variables, Blocked and Block_sight.

I can fill the array just fine, and even instantiate from the objects in that array, however, for the life of me, I cannot seem to be able to pull the variables up no matter what I try.

Any help would be appreciated:


var myself : GameObject;
var blocked : boolean;
var block_sight : boolean;

//This is the funtion on the objects that creates the array
function Makemap()
    {
    	blocked = false;        //this is the variable I'm trying to call
    	block_sight = false;
    	myself = this.gameObject;
    	for(y in range(GameObject.Find("WorldGenerator").GetComponent(CreateWorld).mapHeight))
    	{
    		for(x in range(GameObject.Find("WorldGenerator").GetComponent(CreateWorld).mapWidth))
    		{
    			GameObject.Find("WorldGenerator").GetComponent(CreateWorld).map[x,y] = myself;
    		}
    	}
    }	

var map : GameObject[,];
var isBlocked : boolean;

// And this is the function that checks (for now) if the x, y coordinate in the array has a true blocked value

function Update () {
	isBlocked = map[x,y].blocked;
	Debug.Log("is this a blocked tile?" + isBlocked );
}

I get the following error when I run the game:

NullReferenceException: Object reference not set to an instance of an object
Boo.Lang.Runtime.RuntimeServices.CheckNumericPromotion (IConvertible convertible)
Boo.Lang.Runtime.RuntimeServices.CheckNumericPromotion (System.Object value)
Boo.Lang.Runtime.RuntimeServices.UnboxBoolean (System.Object value)
CreateWorld.Update () (at Assets/Scripts/CreateWorld.js:34)

1 Answer

1

If it’s a script that’s attached to a GameObject, then it’s actually a component of the GameObject.

You would need to use the GameObject.GetComponent(~script name here~).blocked

That was it, thanks a lot mate.

Glad I could help!