Quantcast
Channel: Answers for "What's the difference between SendMessage and GetComponent?"
Viewing all articles
Browse latest Browse all 6

Answer by Molix

$
0
0

Make sure you read that link carefully! It states that GetComponent is "probably at least as slow as" SendMessage. The performance gain comes from caching the reference to the target (i.e. calling GetComponent once in Start()). e.g. (C#)

private EnemyScript enemy = null;

void Start()
{
  enemy = GetComponent(typeof(EnemyScript)) as EnemyScript;
}

void DoStuff()
{
  enemy.ApplyDamage( 20 );
}

Viewing all articles
Browse latest Browse all 6

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>