- Home /
Find all objects that have a specific string.
I have an object that has a string color = "color here"
What I need is to count all objects that have color = "red". Using tags is not an option, there will be A LOT of tags otherwise.
Thanks
what do you mean by "objects that have a specific string"?do you mean the name of the gameobject? or A variable called "color" in the script attached to that gameobject?
Answer by Justin Warner · Apr 13, 2011 at 03:22 PM
Use a loop to loop through all the game objects in the scene.
Give every object a script (You can just put it in the script that has the color thing).
Now, you want to add a getColor(); function, and have that just return the string in the color spot...
function getColor() : String (Or is it string, idr){
return color;
}
So, when you get the color, you check it in an if statement...
var redCount = 0; var blueCount = 0;
if(color = "red"){ redCount++; } else if(color = "blue"){ blueCount++; }
You should get the point from here =).
Good luck!
Your answer
![](https://koobas.hobune.stream/wayback/20220613122530im_/https://answers.unity.com/themes/thub/images/avi.jpg)