- Home /
Transforms are SUPER HARD!
I have seen many people to get the tag/name of the object they want they say :
transform.tag
But that doesn't make sense because isn't it getting the transform's tag (even if there isn't a transform tag) and not the object's tag. Why is the transform component getting the gameobject's TAG.
Why isn't the code gameobject.tag?
UNITY DOCUMENTATION DOES NOT HELP I AM 14 YRS OLD HELP!
i recommend investing a bit more time into basic program$$anonymous$$g concepts and the c# language itself, otherwise many things when working with unity will seem super hard and confusing to you ;)
Answer by Hellium · Jan 17, 2019 at 06:59 PM
The documentation is pretty clear:
public string tag;
Description
The tag of this game object.
https://docs.unity3d.com/ScriptReference/Component-tag.html
The code of the Component.tag
accessor is literally the following ( Transform
inherits from Component
)
public string tag
{
get { return gameObject.tag; }
set { gameObject.tag = value; }
}
So there is no difference in code between the following lines :
Debug.Log( this.tag );
Debug.Log( transform.tag );
Debug.Log( gameObject.tag );
i think the problem is that he hasn't yet learned what inheritance is, so now he knows those three are the same, but he doesn't understand why.
Answer by SteenPetersen · Jan 17, 2019 at 06:04 PM
I understand your confusion, Because technically "Transform" is a component of a gameobject, but it is a component that every single gameobject has, you cannot get rid of it.
You will note however that you can write:
gameObject.tag == "WhateverTagYoureLookingFor"
// or
transform.tag == "WhateverTagYoureLookingFor"
Both will return the same thing.
I could be wrong but I think it is simply because you can always know that a GameObject has a transform and vice versa. Therefore they both have the condition .tag