Accessing GameObjects Color To Store In Variable

What i am trying to do is Change all of the GameObjects that are children of a certain GameObjects color. The problem i am having is that when i try to store the original color of the GameObject i am trying to change, i get an Error: NullReferenceException: Object Reference not set to an instance of an object.

But here is the code :

`
private var menuCube : GameObject[];
private var mainColor  : Color;

function Start()
{	
	menuCube = GameObject.FindGameObjectsWithTag("MenuCube");
	mainColor = menuCube.renderer.material.color;
	
}

function OnMouseOver() 
{
	menuCube.renderer.material.color = Color.green;
}

function OnMouseExit()
{
	menuCube.renderer.material.color = mainColor;
}

`

The Error shows up at line 8:

`
mainColor = menuCube.renderer.material.color;
/* This line is where the error is pointing */
`

Just to clarify i intend this script to be attached to the parent object.

You are getting the NullReferenceException because you’ve created your ‘menuCube’ variable as an array

private var menuCube : GameObject[];

When you need to access your ‘menuCube’ var, you need to also tell it which index.
ie: the first item in the array would be: menuCube[0]
the 2nd would be menuCube[1]
the 3rd menuCube[2]
etc, etc.

Also, I don’t believe your script will work anyway as the OnMouseOver will only get called when the mouse is over your parent object. If your parent object is an Empty GameObject the the OnMouseOver function will never get called.

If you want to use OnMouseOver() in a script then that script must be on each object you want it to effect.