- Home /
once again, Sendmessage VS Getcomponent!
i saw a lot of questions and post about this topic, most of the SCREAM don't use sendmessage because it way more slower then events or even getcomponent. as far as i understand events are good to broadcast messages to several receivers at once and getcomponent can send message to specific gameobject.
so for scenario where a bullet need to send damage message to specific gameobject that got hit i first used sendmessage because it was easy and fast to implement. but after i saw these articles and posts i replaced it with getcomponent and to my surprise i got huge performance impact. while asking myself WTF?!.. i roll back to sendmessage.
after a bit more reading i found that getcomponent is fatser when you cache the component you get and then use it, but in my case it useless since the gameobject\bullet destroyed after they get hit and i need to cache the component on every hit.
to my question,what other fast methods can i use with my scenario? ( a reminder, a lot of bullets need to send damage message to an object that got hit)
without seeing your code it's a bit of a guess, but ...
why not cache the reference to the object and have the bullet handle damage, etc. before it gets destroyed?
Yeah the simple way for me is the same than gif. Cache a ref to your component apply your dmg and co. then destroy it.
i'm pooling the bullets so they not exactly destroyed. also i'm using single bullets type that can make several amounts of damage depend on the shooter. the chain is: shooter setup the bullet properties -> bullet apply damage according his properties.
so, when the same bullet used for both player and several type of NPC, i can't cache them all.
but what is the point of caching component if you only use it once? remember that a bullet can hit several type of objects so every hit it need to cache again the current object that got hit.
Answer by whydoidoit · Jul 09, 2014 at 01:52 PM
SendMessage is fine for inflicting damage as long as it isn't happening every frame, the impact is much lower. Without seeing your GetComponent code I can't comment, but it is surprising that this caused a noticeable performance hit which implies it might have been coded oddly.
i don't have a profiler but i could see the impact when the render time jump from 0.2ms to 0.4-0.6ms. for the code, i'm not near my home computer but i will post it later...
i decided to stay with sendmessage since i don't use it every frame and the performance are good. thanks for your answer!