Container
contains Trigger (tagged “Fungo”)
which in turn contains Mushroom (tagged “SubFungo”).
Why is it that, from the script attached to container, a call to FindGameObjectsWithTag does not find any item tagged “SubFungo”? How can I manage to solve this?
GameObject (untagged, with script)
—>GameObject (tagged:Fungo)
------>Sphere (tagged:SubFungo)
the script I put on the parent to test was the following:
var obj_name: String;
var search_tag: String;
function Update () {
var found = 0;
try {
var results = GameObject.FindGameObjectsWithTag(search_tag);
for (var object in results){
obj_name = object.name;
found++;
}
}
catch (notdefined){
return;
}
if (!found)
obj_name = "no object found";
}
This allows you to test various tag names to search for in the hierarchy, using the inspector as a debugger while the game is running.
I didn’t have any problems finding SubFungo in this simple example.
Are you using the same Capitalization scheme for your search and the tag?
Try using the test script and seeing if it doesn’t find the object
Silvia, I sort of converted the above script to C# and it worked for me. When I made a public string search_tag and tried to type the name in the editor I got an error saying SubFungo not defined, so I just hard coded the search string to be SubFungo. I’m brand new to Unity so I’m not sure why I get that error. I also didn’t bother with the try/catch block as it’s not really necessary.
EDIT: I got the public string search_tag to work in the editor, I just had to restart Unity. If you use the search_tag variable in the editor you should change the Awake function to Update so you can see the results change on the fly.
using UnityEngine;
public class Test : MonoBehaviour {
public string obj_name;
private GameObject[] results;
private int found;
private void Awake() {
found = 0;
results = GameObject.FindGameObjectsWithTag("SubFungo");
foreach (GameObject obj in results) {
obj_name = obj.name;
found++;
}
if (found == 0) {
obj_name = "object not found";
}
}
}
Since the function continuously looks for the tag equal to your input string, it should be expected to return an error while you’re still typing it out. This is because it will look for the tag “S” then “Su” then “Sub”, etc, while you enter it in.
This error is handled with the Try / Catch section, and is the reason it is there. It tries to find a tag, and if it’s undefined, the catch tells it to go about its business. I recommend including it.
If your trials with the C# test-find have been successful, then it might be a capitalization or spelling error?
In addition, make sure that you actually made new tags, and not new layers.
When you select “Add Tag”, the most visible options are empty layers.
To add an actual tag, you’ll have to expand the “Tags” variable at the top of the list of layers, and add a string into an empty element.
It was just that Unity needed to be restarted. After I got the errors with the string it started complaining about being unable to open temp files and then it crashed. After I restarted it I tried the exact same thing again and it worked fine. You do get a brief error as you’re typing in the string (which I expected), but once the full string is entered the error goes away and it finds the game object.
If you were actually doing this in game instead of testing in the editor you obviously wouldn’t use the update function, and try/catch blocks slow things down so I avoid them except in cases that would actually stop the game from running.