# Help with understanding “transform.root.GetComponent<>” in Unity

I'm reading through a tutorial for making an RTS in Unity, and in certain scripts it wants you to make a Player field and then OnStart(){ transform.root.GetComponent}. I am not quite getting what that does or what purpose it serves.

Player.cs is added as a component to the game object "Player". Player.cs is the following:

public class Player : MonoBehaviour {

public string username { get; set; }
public bool human { get; set; }
}


Then on another script which is also attached to the Player (gameobject) as a component:

protected Player player;

protected void Start ()
{
player = transform.root.GetComponent<Player>();
}


This is instructed: http://stormtek.geek.nz/rts_tutorial/part2.php right under "Camera Input". There is a description there, but I am confused by it since it does not allow you to actually interact with the game object Player, but the instance of the Player script.

I'm sorry if this was poorly worded, I can't think of a better way to ask this question. I am completely confused as to why it asks you to do this and what it actually does. I'm also very confused about how to reference instances of a class that are attached to a game object, and if the snippet in question assists with that (I can remove the snippet and things seem to work fine).

Edit: I have looked at the unity documentation on game objects, transform, root, getcomponents and other related methods.

I am not quite sure what you are not understanding here, but I attempt to answer your question anyway.

I am not quite getting what that does or what purpose it serves.

In cases where you do not fill in the fields in the inspector (which requires them to be public), you need to find the instances of the needed components in code. The GetComponent<T>() method does just that; it tries to find the given component instance from the game object the method is called on. In this case, it tries to find the Player component from the topmost game object (root) in the object hierarchy.

You can also get the component instances by making the field public and dragging the correct object in to the field in the inspector in Unity editor. In this case you do not have to call the GetComponent method, but you need to make sure it is actually set before you try to use it. If you do not, the game will throw a null reference exception.

The null check is recommended thing to do in any case, even when you get it with GetComponent method.

This is instructed: http://stormtek.geek.nz/rts_tutorial/part2.php right under "Camera Input". There is a description there, but I am confused by it since it does not allow you to actually interact with the game object Player, but the instance of the Player script.

The GameObject does nothing by itself. It always has some components that actually does something. The component can determine if it should draw some mesh, or some sprite, or just play some sound. Without any components, the empty GameObject is pretty much useless entity in the scene. The component scripts derived from MonoBehaviour class are used for custom behaviour for the GameObject, in addition to the Unity's built-in components.

The GetComponent<T>() again is the key here, it always gets the instance of the component script for you to interact with. If you modify something in this instance, it will modify only the GameObject's component.

I'm also very confused about how to reference instances of a class that are attached to a game object, and if the snippet in question assists with that (I can remove the snippet and things seem to work fine).

It works fine if the player field is not accessed anywhere in your script code. It will not work if you try to interact the Player script from the code if you have not assigned it's instance to the player field.

• Ah I get it now, thanks a ton! So, if I get the instance of that components script can I "get" information from that script? ie. I can call public methods from that script? – Douglas Gaskell Jan 24 '15 at 23:43
• That is correct. – Lasse Jan 24 '15 at 23:50