Change colour of child

Hi there

I have a script that instantiates a prefab. The user can move the mouse over a colour palette and change the colour of part of the prefab that has been instantiated.

How do I access part of the prefab? The model is a car - and I want the user just to be able to change the colour of the body - not the wheels etc

thanks
Steve

Hi Steve

I had a similar question to gain access to a child of an instantiate object.

See bottom of this post: http://forum.unity3d.com/viewtopic.php?t=30692

Might help.

Thanks
Geoff

thanks for your reply.

I still not sure how to do this.

I have a game object with a script component attached.
From this script a prefab is instantiated - a car. I have targeted the prefab in the script with:

var colcar : Transform

It gets instantiated as

ColourCar (prefab)

  • BR (wheel mesh)
  • BL (wheel mesh)
  • FR (wheel mesh)
  • FL (wheel mesh)
  • Body (body mesh)

would I use?:

colcar.Transform.Find(“Body”).renderer.material.color = col

I still cant get this to work. Please help.

Trying to colour a model chassis (a child in targeted variable “PlayerPreb”).

But the chassis loses its intial red material, becomes grey, and doesnt change colour when the mouse moves over the palette.

Here is the rough script:

var PlayerPref : Transform;
var palette : Texture2D;

function Start()
{Instantiate(PlayerPref, new Vector3(1,1,1), Quaternion.identity);}

function Update()
{
var MouseCol = palette.GetPixel(Input.mousePosition.x, Input.mousePosition.y);
PlayerPref.transform.Find(“fincar/Mesh1_Model”).renderer.material.color = MouseCol;
}

You need to assign the return value from instantiate to get a reference to it the new object created.

e.g.

var objectToInstantiate : Rigidbody;

var someObj = Instantiate(objectToInstantiate, transform.position, transform.rotation);

Now you have a reference to it as someObj and can assign properties to it like the name.

e.g.

someObj.name = "MyNewPart";

now if you want to change its colour at a later point somewhere you can do…

GameObject.Find("MyNewPart").renderer.material.color = Color.red;

Hope that helps. Its late and ive drunk lots of stella so could be a load of bollox :slight_smile:

Geoff

awesome - it worked!! - good choice of brew too - Stellar is great.

thanks