Not sure what to do here. I have multiple types of gameObjects that share common color tags. So I want to count all the “Boxes” tagged “Red” - This is what I thought I needed to do; but doesn’t work.
//CountWithTag
static function CountTypeWithTag(game_Token, thisTag) {
//get all the same type gameObjects
gameTokens = FindObjectsOfType(game_Token);
//loop over the result and store in an array
var taggedGroup = new Array();
for (var gameToken in gameTokens) {
//pick out all the ones of thisTag
if (gameToken.tag == thisTag) {taggedGroup.Add(gameToken);}
}
//then return gameObjects with tag
return taggedGroup.length;
}
Thanks for reading, and even more for helping 
-Cheers,
try changing: if (gameToken.tag == thisTag) {taggedGroup.Add(gameToken);
to: if (gameToken.gameObject.tag == thisTag) {taggedGroup.Add(gameToken);
also, try printing gameTokens.length.
Is it possible to pass types through functions like that? How do you call it?
TomsScript.CountTypeWithTag(CubeToken, “red”);
Like that?
Error Message:
Updated Code (no longer static):
//CountTypeWithTag
function CountTypeWithTag(game_Token, thisTag) {
//get all the same type gameObjects
gameTokens = FindObjectsOfType(game_Token);
print ( "Number of Game Tokens" +gameTokens.length );
//loop over the result and store in an array
var taggedGroup = new Array();
for (var gameToken : GameObject in gameTokens) {
//pick out all the ones of thisTag
if (gameToken.gameObject.tag == thisTag) {taggedGroup.Add(gameToken);}
}
//then return gameObjects with tag
return taggedGroup.length;
}
Called Via:
CountTypeWithTag(Candy, "Red")
Where Candy is defined and assigned via inspector as…
var Candy : GameObject;
Then I tried where Candy is only the name of the script that a gameObject has – Candy.js (see error above)
…if someone’s will have problems with identify the objects…
I wrote (C#)
Component []ListObj=FindObjectsOfType(typeof(your object type)) as Component[];
if (ListObj==null){
print("no way");
}else{
int parkll=ListObj.Length;
for(int i=0;i<parkll;i++){
if (this.name=="Main Camera"){
print(ListObj[i].name);
if (ListObj[i].name=="the name"){
<your code> return;
}
}
}
}
If U connect a script to an object , (I connected to main camera…I supposed was for this) the script will be executed as many times as many are the objects.So , I resolve the problem with a ol’ good
if (this.name==“the name of the object U connect the script”){
execute the script
}else{
return
}
Try and hope!