- Home /
Count Objects with specific colour
Hello Unity Answers. In my scene I have 20 cubes for witch I specify a colour through java script on mouse click. Each of them have a tag "Tile". How do I filter GameObjects[] list to count objects with lets say "blue" colour and perform an action when it's less than specified amount?
Answer by Nick4 · Mar 08, 2014 at 10:44 AM
var counter : int;
var specifiedAmount : int;
function Update()
{
counter = 0;
for(var go : GameObject in GameObject.FindGameObjectsWithTag("Tile"))
{
if(go.renderer.material.color == Color.blue)
{
counter++;
}
}
if(counter < specifiedAmount)
{
// Amount of blue objects is less then specified amount.
}
}
This will do it for you. Just make sure those objects are really blue. If you're not sure what color they actually are, find out then assign it to a variable that is type of Color and change this line.
if(go.renderer.material.color == Color.blue)
to this
if(go.renderer.material.color == MyCustomColor)
Good luck.
Your answer
Follow this Question
Related Questions
Changing two different objects renderer colour 1 Answer
Are Mesh Filters Really Necessary? 1 Answer
appear/disappear gameobject 3 Answers
I cant turn off my mesh renderer for some reason 3 Answers
SetFloat doesn't work 1 Answer