- Home /
SendMessage or GetComponent
Hy Folks, I have following question:
I work on a mobile project where I often have to call functions on temporary instantiated gameObjects. I think I am already familiar with all common ways to do this, my question is more reguarding performance, for example(cs):
currentWeapon.GetComponent<WeaponScript>().SetAmmo(30);
currentWeapon.SendMessage("SetAmmo",30);
Does anyone of you know which is the cheaper way to do this, or is there an even better solution. Is SendMessage depending on how many scripts are on the called gameObject?
Thanks in advance,
anszwa
Answer by Eric5h5 · Nov 15, 2012 at 08:16 PM
GetComponent is faster than SendMessage, but they do somewhat different things and are not really all that interchangeable in most cases. They both have a cost depending on how many components are attached. If you want the absolute best performance, you should not use the generic form of GetComponent, though it's unlikely it will actually make a measurable difference in practice. If you're frequently accessing the component, then do GetComponent once and cache the result.
Your answer
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Accessing other objects efficiently 1 Answer
once again, Sendmessage VS Getcomponent! 1 Answer
Change the Halo Component (not the default) in a light in C#??? 0 Answers
WorldToScreenPoint in Update/OnGUI: Bad Performance 1 Answer