Aleksa Ristic
Aleksa Ristic

Reputation: 2499

Does GetComponent<>() impact performance

Just as title say does GetComponent() does impact a lot on performance.

I am asking this because I do not like doing it like this:

public class Player : MonoBehaviour
{
    PlayerStats playerStats = this.GetComponent<PlayerStats>();

    void Update()
    {
        var something = playerStats.Asd;
    }
}

Instead of that i like using it like this:

public class Player : MonoBehaviour
{
    void Update()
    {
        var something = this.GetComponent<PlayerStats>().Asd;
    }
}

Reason for that is because i like breaking code in lot of scripts (it is easier for me to later change something if needed and also use one script for multiple objects) and so if i have a lot of scripts i need to see if i have already defined PlayerStats playerStats.... but not only this one but about a lot of them.

So is using second approach going to slow down my game a lot?

Upvotes: 5

Views: 8465

Answers (3)

user9622872
user9622872

Reputation:

The main appeal of using the first approach is that you can set those variables to be public, and in turn access them directly from the Unity Editor, allowing you to drag and drop components as you feel like it.

The second instance of your GetComponent function call means that you aren't caching your variables, slowing down your code with potentially unnecessary checks. So my advice would be to stick with the first instance in which your variable is defined in memory and then altered, rather than being allocated memory each time and then altered.

And a side note. You do not need to call this.GetComponent if the script is attached to an object since the script derives from a MonoBehaviour; you can just call GetComponent<type>() and go about your merry day. :)

Upvotes: 7

AustinWBryan
AustinWBryan

Reputation: 3326

I don't think it really matters. I just did a check and using for loop that looped 1,000,000 times and found the exact same 0.02 time delay between both frames.

That being said, it would make your code cleaner because Player.Stats.Asd is cleaner than Player.GetComponent<PlayerStats>().Asd. It makes it more obvious what the intent is. I'm sure it is still a micro optimization to store it as a variable with a public PlayerStats Stats { get; set; }, but that's really if you're using it all the time.

You shouldn't use a variable for every Component it has, because if you do that for every script, the memory being used will start to add up.

Also, note that I'm calling it Stats not PlayerStats because Player.PlayerStats is needlessly redundant. The actual type of it should be called PlayerStats yes, to not confuse it with, say, EnemyStats, but when you go to use both, having Player.Stats and Enemy.Stats is cleaner.

Upvotes: 5

Programmer
Programmer

Reputation: 125245

It's worth noting that your first script is invalid and won't compile. The correct way to do that is to make it a global variable but cache or initialize the script in the Start or Awake function. These functions run once and are used for initialization.

Something like this:

PlayerStats playerStats;

void Start()
{
    playerStats  = this.GetComponent<PlayerStats>();
}

To answer your question, the GetComponent function impacting performance is greatly over exaggerated. You read this everywhere on the internet but it depends on how often it is used. If it is used once in a while or just in few scripts then it's totally fine.

If you have hundreds instances of scripts using GetComponent in the Update function then that's when you will start noticing a little bit of performance hit because GetComponent is making call to the native side. So, it depends on how often it is called and how many instances of scripts are making this call each frame.


Upvotes: 9

Related Questions