is "tag" a component?

According to the Unity docs “tag” is a component Unity - Scripting API: Component

but the following code will cause an error:

var children : tag = gameObject.GetComponentsInChildren(tag);
children = "childTag";

The name ‘tag’ does not denote a valid type (‘not found’). Did you mean ‘UnityEngine.Touch’?

Basically I am trying to tag the children of a prefab, without having to know the names of the children.

http://unity3d.com/support/documentation/ScriptReference/index.Accessing_Other_Game_Objects.html

Be careful; that’s the impression you’re getting, but that’s not true. It just happens that many of the “variables” of Component (all except gameObject and tag) are Properties that return components. To be clear, it would be better if they all started with capital letters, because lowercase suggests that you actually have a stored reference to the component, when in reality, the “variables” all run “GetComponent” behind the scenes.

An GameObject’s tag is a string. If you want to set the tags of all child objects, you can use something like the following code.

for (var child : Transform in transform) {
    child.tag = "Some Predefined Tag";
}

I read through this a few times while trying to cook up the code to assign tags, it doesn’t provide any example for setting tag names to child objects in code, however. Accessing this property and changing it is the end-goal.

Thanks for the code example Jasper, I’ll check this out.

I have this working now, thanks a million. (however, grandchildren are unaffected)

Anyone know how to access the layer of a child also? Sorry to deviate from the thread title.

I tried:

for (var childl : Transform in transform) {
    childl.layer = 11;
}

Obviously doesn’t work, something like this is out of the question too:

var moveLayerC : GameObject;
moveLayerC = gameObject.GetComponentsInChildren(layer);
moveLayerC = 11;

I’m only asking because child tags and layers are not inherited when dynamically changed to the parent using gameObject, and the documentation on this is lacking (or rather; my interpretation of the existing documentation), and would robustly like to know how to access tags and layers of children, without knowing their names.

Figured this one out over at Unity Answers, for those that are interested, how to change a child’s layer:

child.gameObject.layer = LayerMask.NameToLayer("layerName");

Would be neat if tags and layers were components. All fixed now :wink:

or just child.gameObject.layer = 10;

No wouldn’t be neat actually it would be even worse than it is at the time.

Tags are already a “enjoy with caution” topic cause strings generate a lot of garbage so constantly working with tags can impact webplayer performance but especially on mobile it can easily become a performance killer.

Layers luckily are numbers which don’t generate trash thus are favorable if you can do it that way or alternatively having an own manager to which objects register upon startup can and should be used given you can’t use “startup caching” naturally

much simpler, thanks.

That’s what CompareTag is for, so you don’t have memory allocation (and it’s faster than string comparisons).

–Eric

Most of my statements are based on complete ignorance. Wasn’t that obvious how to tag and layer child objects from unity documentation. Thanks for the advice guys.