- Home /
How to change colour of a child object
I have a series of objects laid out in a grid and I want to select one of these objects at random and change their colour. In the hierarchy the objects are laid out like this:
city
-Line 1
-Building 1
-Building 2
-Building 3
...
-Building 23
-Line 2
...
-Line 23
So each line has 23 buildings and there are 23 lines. I want to produce two random numbers, x and y, then I want to go to line x and building y in that line and change the colour of that object. So I have two queries. Firstly how do you change the colour of an object and secondly how I traverse the hierarchy and access a child of a parent? Thanks
Are "Line" and Building" just GameObjects with $$anonymous$$eshFiter and $$anonymous$$eshRenderer components? or are they classes?
line is an empty object but building has meshFilter and renderer components
Answer by Owen-Reynolds · Dec 27, 2014 at 04:58 PM
The real problem is that, even though you've written it as two Q's, in your mind it's still one Q. So you can't focus on each Q by itself.
You can easily look up how to find a child object. It's transform.Find
and there are plenty of examples.
And, you know you can look up how to change color. It depends on the exact material and shader. But generally just B.renderer.material.color
(where B is whatever object.)
ok after doing very little research I solved my problem, turns out it was easy. For whatever reason I thought the problem was more difficult. Thanks :)
Answer by ExtremePowers · Dec 27, 2014 at 01:07 PM
Try this:
for (var child : Transform in transform) {
var RandomColor = Color(Random.Range(0.0f,1.0f),Random.Range(0.0f,1.0f),Random.Range(0.0f,1.0f));
child.renderer.material.color = RandomColor;
}
Is there any way you can explain this code and maybe translate it to c#. The latter isn't necessary :)
C#:
//Loops through all the children of the gameobject's transform
for (Transform child in transform) { //You can use transform.root too, which always gives the "$$anonymous$$aster parent"
//Generates a random color by generating 3 random numbers between 0.0 and 1.0.
Color RandomColor = Color(Random.Range(0.0f,1.0f),Random.Range(0.0f,1.0f),Random.Range(0.0f,1.0f));
//Assigns the materials color to be the random color you generated above.
child.renderer.material.color = RandomColor;
}
hmm ok. So this loops through all child objects of the parent and assigns them random colours. Right? If that's right then I didn't explain myself clearly, sorry. I want to produce two random numbers and then go to that specific "building" and change only that building's colour I was thinking of something along the lines
int x = Random.range (1, 23);
int y = Random.range (1, 23);
//here is the bit I don't know so sudo code
gameObject get ("Line " + x);
{
gameObject get ("Building " + y);
{
//assign colour red to this object
}
}
I know this is gibberish but I hope it better explains what the problem is. Thanks
Your answer
Follow this Question
Related Questions
Fast layer assignment 3 Answers
Destroying a prefab doesn't remove it from the Hierarchy list 2 Answers
Selecting transform in FBX hierarchy csharp script 1 Answer
Changing the hierarchy through code 0 Answers