- Home /
Array with game object: object reference not set to an instance of an object
I have a script which genrates 2d grid out of cubes. Before I made it with 2 for's and tile = Instantiate (tilePrefab, Vector3(j, i, 0), Quaternion.identity);
I wanted to easier control those tiles so I made array:
var tileList : GameObject[,];
tileList = new GameObject[y,x];
tileList[j,i] = Instantiate (tilePrefab, Vector3(j, i, 0), Quaternion.identity);
affter that I check in some other array what type is that tile so that I would assing a color to it. It worked fine before but not I get: object reference not set to an instance of an object.
Code is like this:
if(tileGrid[i,j]<15)
{
tileList[i,j].material.color = Color.green;
}...
This is in same for loop like that code before. Before I started storing tiles in array it wored perfectly.
Could you be more specific about exactly where the for-loop is? By the looks of things, you're rebuilding the entire array every iteration! Could you tell us where the brackets are (since I think that's the important detail, here)?
Ok I will post the whole code but it's quite some of it I think.
var tilePrefab : GameObject;
var y : int = 5; var x : int = 5;
var initialColor : Color;
var tileGrid : int[,]; var tileList : GameObject[,];
function Start (){
tileGrid = new int[y, x]; tileList = new GameObject[y,x]; rand$$anonymous$$ap();
for (var i : int = 0;i < y; i++) { for (var j : int = 0; j < x; j++) { tileList[j,i] = Instantiate (tilePrefab, Vector3(j, i, 0), Quaternion.identity);
if(tileGrid[i,j]<15)
{
tileList[i,j].material.color = Color.green;
}
else if(tileGrid[i,j]<18)
{
tileList[i,j].renderer.material.color = Color.blue;
}
else if(tileGrid[i,j]<20)
{
tileList[i,j].renderer.material.color = Color.gray;
}
else if(tileGrid[i,j]<22){
tileList[i,j].renderer.material.color = Color.cyan;
}
else
{
tileList[i,j].renderer.material.color = Color.yellow;
}
if (i == y/2 && j == x/2)
{
//print("Found middle of the map");
cameraPosition= tileList[i,j].transform.position; // get the tile position...
cameraPosition.z = Camera.main.transform.position.z; // but keep the camera z
Camera.main.transform.position = cameraPosition; // move camera to the position
tileList[i,j].renderer.material.color = Color.magenta;
}
}
} }
function OnGUI () { var e : Event = Event.current; if (e.is$$anonymous$$ouse){ if($$anonymous$$eyCode.$$anonymous$$ouse2 ){ mousePositon = Vector3(Input.mousePosition.x,Input.mousePosition.y,Input.mousePosition.z); cameraPosition = Vector3(Input.mousePosition.x,Input.mousePosition.y,-25.0); Camera.main.transform.position = cameraPosition; Debug.Log("$$anonymous$$ouse position is:"+mousePositon); Debug.Log("Camera position is:"+cameraPosition); }; };
Answer by Jure · Jan 11, 2012 at 09:28 PM
I got it answerd on IRC chat. I had to change code in IF statment to somthing like this: tileList[i,j].renderer.material.color = Color.green;
Your answer
Follow this Question
Related Questions
Missing (Game Object) 3 Answers
Make object move in a direction depending on where it spawns? (C#) 1 Answer
Updating a component on a NetworkView game object doesn't update the component on other clients 1 Answer
Different array value at different times 1 Answer
Prefabs instantiated from an array are keeping their public int value 1 Answer