When to use GetComponent<T>()

I’m a little confused about when we need to use GetComponent or when we can just access the field. For instance, in Block.cs to access the sprite we use

GetComponent<SpriteRenderer>().sprite = hitSprites[spriteIndex];

However, when we need to access the position of the block in order to instantiate the visual effect we can directly access transform.position.

Why couldn’t we have said SpriteRenderer.sprite = hitSprites[spriteIndex]?

Hi Dithier,

Imagine the following: You create 10 game objects with a Sprite Renderer and a Block component attached to each of them.

To which of the 10 Sprite Renderer components does your SpriteRenderer refer?

But I don’t see how that’s any different than the GetComponent. If you do GetComponent you aren’t specifying which sprite renderer component you are referring to, just like if you directly access SpriteRenderer you aren’t specifying. I don’t see how one is better than the other. I’m assuming that the script references the component of the object it is on. Also, we have multiple game objects of blocks with different positions yet we still in the block.cs script just said transform.position

There are several reasons but the most dominant reason is for Reuse-ability and Easy of the script.

If I write a script that says:

void Start()
{
   _object = GetComponent<SpriteRenderer>(); 
}

Then no matter what object I drop this script on it will get the first SpriteRenderer Component, I will not have to drag anything from the editor. Can save time when using it on a lots of objects.

Then there are times when you do not know what object is going to get passed in and you want to see if it has a component if it does you use it.

void OnCollisionEnter2D (Collision2D col)
{
  var go = col.gameObject.GetComponent<IDamage>(); 
  if( go != null )
  {
    // Do Something
  }
}

You cannot just use a field in every instance, you need to know how to access that info when your unsure what object your going to get.

if you want to access more then one you use:

var renderers = GetComponents<SpriteRenderer>();

That will return an array of SpriteRenderers then you can access the individual SpriteRenderers by index.

renderers[index]

member field access is ideal but not always what you will be able to do. Use the tools that best help you to solve your issue. If it works then it works, there is rarely ever just one solution. It typically comes down to the situation, performance, and skill level.

As for your question:

Why couldn’t we have said SpriteRenderer.sprite = hitSprites[spriteIndex] ?

SpriteRenderer is a class. When you use a class and say

SpriteRenderer.sprite

what you are doing is calling a static member of the Class. If you are not familiar with static member and static classes you can do a quick search but your not affecting the particular SpriteRenderer that is in the object you are in fact accessing an instance that is on the heap somewhere an persistent to the Class itself.

Most likely that would cause a compiler error because I do not think that SpriteRenderer has a static field called sprite. You can look it up though if you want it would be a good exercise.

The GetComponent<T>() returns an Object of T.
The GetComponent<SpriteRenderer>() returns a specific instance of a SpriteRenderer Class that you are then setting.You can expand the original line like this.

var go = GetComponent<SpriteRenderer>();
go.sprite = hitSprites[spriteIndex];

Yes, you do. When you call GetComponent, the method looks for a component attached to the same game object as the current script. That is very specific.

someOtherGameObject.GetComponent would return the component attached to the game object referenced by the someOtherGameObject variable.

When you assign a script to a game object, e.g., block.cs to a Block game object, that script becomes an individual component. If you assign 100 block.cs scripts, you will get 100 individual components.

Due to the inheritence from MonoBehaviour (see your script), you have a variable called transform.position. The transform variable references the Transform component of the game object to which your current script is attached.

Ahhh, I understand now. I didn’t realize Monobehavior had a transform property. Thanks!

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms