Question about tags

I have this kind of hierarchy:

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?

Thanks a lot.

I made an empty scene with that same hierarchy;

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

I forgot to mention that I write in C#. I don’t know whether it makes any difference…

(By the way, I tried to translate the script above in C#, but it didn’t seem to work. Probably I did a poor translation, however.)

Edit: the script above WORKS. I’m quite puzzled with the C# equivalent.

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.

btw Silvia if you want to see the c# equivalent of the js code above, complete with try/catch etc, here you go:

using UnityEngine;

public class Test : MonoBehaviour {
	public string obj_name;
	public string search_tag;
	
	private GameObject[] results;
	private int found;

	private void Update() {
		found = 0;
		try {
			results = GameObject.FindGameObjectsWithTag(search_tag);
			foreach (GameObject obj in results) {
				obj_name = obj.name;
				found++;
			}
		}
		catch (UnityException e) {
			Debug.Log(e.Message);
		}
		if (found == 0) {
			obj_name = "object not found";
		}
	}
}

I think I also discovered why Unity freaked out and crashed, I was saving changes to my code while the game was running…that makes Unity unhappy!

Ah, I misunderstood.
That makes perfect sense, though I wasn’t aware try/catch would have a notable effect on performance. I will keep this in mind!