Accessing Specific Children to Change Color

What i am trying to accomplish is every-time my player touches a cube it changes the color of the cube that is touched.

In this case the color would be red.

The Problem i am having is when trying to use “gameObject.Find()”

when i use this it finds ONE game object and turns the color to red.

Here is the code:

`
    function OnCollisionEnter(theCollision : Collision) {
	    
	Debug.Log("Hit");
	
	if(theCollision.gameObject.name == "Cube_Neut")
	{
	
	var theCube = gameObject.Find("Cube");
	theCube.transform.renderer.material.color = Color.red;
	
	Debug.Log("Changed Color");
	}
	
	
	
}
`

The thing is that my “gameObject” was imported and has two children because there is a rim (if you want to call it that) around the corners of the cube and the regular cube itself.

So in the Hierarchy it looks like this:

`

       Map_1   (Map_1 representing the level as a whole)
         -Cube_Neut (Child of "Map_1")
            -Cube  (This is the part i want to turn Red. Child of "Cube_Neut")
            -Cube_006  (This is the black rim of the cube that i want to keep
the same. Child of "Cube_Neut")

           
`

I am trying to find a better way of doing this so that is why i am here.

Any Help is Appreciated

You already has got a reference to the hit object in theCollision. You can find only the child transform you want using theCollision.transform.Find(“ObjectName”):

function OnCollisionEnter(theCollision : Collision) {
  if (theCollision.gameObject.name == "Cube_Neut"){
    var theCube = theCollision.transform.Find("Cube"); // theCube is a Transform
    theCube.renderer.material.color = Color.red;
    Debug.Log("Changed Color");
  }
}

Only the hit object’s child named “Cube” will change its color - the “Cube_006” will not be affected, nor other “Cube” childed to other “Cube_Neut” objects.