- Home /
SendMessage Slows down Unity Dramatically
So i have been working on a game for awhile and SendMessageUpwards has never been much of a problem for me. The script has a variable, containing the main camera of the scene, called playerCam.
playerCam.SendMessageUpwards("Message", gameObject);
this line incredibly decreases the speed at which unity runs. Anybody know a solution to this?
Answer by Datael · Jun 04, 2012 at 01:56 AM
I hate to state the obvious, but not using SendMessage sounds like a start to a solution to me!
We did tests in work and it takes about 100x the time to call a SendMessage than calling the method directly.
There's plenty of ways around this but it will involve recursively searching parents of parents; I'd probably use some kind of Interface coupled with (also slow, but not so awful as SendMessage) a GetComponent to call the method.
SendMessage takes so long because it uses reflection, which is slow, to check all MonoBehaviours on the target (iirc). SendMessageUpwards does this to all parents, so it's even slower than SendMessage unless it's in the root of your scene, and slower still as you get deeper and deeper in the hierarchy.
If you're only using SendMessageUpwards because you want to tell one object a specific thing then you have two really easy options:
If you're instantiating these objects in code then add a reference to the target in the MonoBehaviour that you're calling SMU from, and when you instantiate the object(s) set the target as you need.
If you've got these objects in your hierarchy already then add serialized (inspector-assignable) properties to them and assign the target in edit mode.
You may be able to use a variation on this using an array if you have more than one target.
Food for thought anyway...
Send$$anonymous$$essage is garbage; there are many other features in place, in mono, that make it unnecessary. I don't know why it was created.
i use Send$$anonymous$$essage because that is always how i have done it. However i see your point. I tried using other methods, i probably just didnt understand them very well, and they failed. So i figured out my problem and that is that Send$$anonymous$$essage should never be under an update function. That was what was slowing my computer down. But those are good methods for me to try later so thank you for the answer.
@DGArtistsInc You're welcome.
I agree with @Jessy in 99% of situations. It is always easier to write a Send$$anonymous$$essage, obviously, since it doesn't require any kind of inheritance to be able to use it, and it is therefore simple, which is why so many people use it. I have had many arguments with people at work because they (used to) use Send$$anonymous$$essage with the DoNotRequireReceiver flag all the time leaving many many Send$$anonymous$$essage calls that did absolutely nothing since their receiver was long-since gone and served to do nothing other than slow the code down (it was literally like we had a load of random WaitForSeconds() sat in the middle of our code everywhere).
I did, however, find myself writing something using a Send$$anonymous$$essage the other day because it was in just one single situation that would only ever happen once during the entire course of the application running (it was a part of a tutorial in a game) and I didn't see the merit in making the function public just for that one situation.
@Berenger Firstly you can subclass, that's the most obvious one. Other examples include (I admit these are C#; I haven't ever touched UnityScript so I don't know how widely these apply) calling a function directly by using GetComponent().function, you can use delegates which allow you to register what actions to do when a certain event happens, or you could use Interfaces if inheritance does not make sense.
http://answers.unity3d.com/questions/243876/efficiency-of-sendmessage.html here was an answer to this topic. There is also a link to a benchmark that shows quite well why sendmessage should be used with moderation.
Answer by angelodelvecchio · Dec 30, 2014 at 08:07 PM
Probably you dont need to send the object every frame, you could do a variable like
var lastObjectSent: GameObject
if( lastObjectSent == thisObjectToSent ) return; this idea helped me a lot
Answer by Kiwasi · Dec 30, 2014 at 11:40 PM
Since this is on the front page again it's worth noting that SendMessage has been replaced by the event system from 4.6 onwards. The event system uses interfaces to allow calling of methods on other GameObjects.
Your answer
Follow this Question
Related Questions
A node in a childnode? 1 Answer
SendMessage to another specific GameObject? 1 Answer
Raycast is being unreliable 1 Answer
How to send sms from unity in ios platform 1 Answer
Why is 4.1.2 so slow for Android? 1 Answer