- Home /
When changing the color of an instantiated object, I keep getting an error - how do I fix this?
I have a couple of pawns I am instantiating and trying to change the color of so that they are more distinct from each other. However, when I try to actually change their color with Renderer.Material.Color = Color.Red, or another equivalent, I keep getting an error from the console:
NullReferenceException: Object reference not set to an instance of an object;
Transform pawnTwo = gridArray [(gridY - 1), (gridX - 1) / 2] as Transform;
pawnPositionTwo = new Vector3(pawnTwo.position.x, pawnTwo.position.y + 10f, pawnTwo.position.z);
Transform pawnObjectTwo = Instantiate(Pawn, pawnPositionTwo, pawnRotation) as Transform;
pawnObjectTwo.gameObject.renderer.material.color = Color.red;
It's almost like there is a timing error, I'm not sure what else is going on. Note that the code is experimental, it'll be refined later.
Comment