Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by DGArtistsInc · Jun 04, 2012 at 01:19 AM · slowsendmessage

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?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

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...

Comment
Add comment · Show 6 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Jessy · Jun 04, 2012 at 02:01 AM 1
Share

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.

avatar image Berenger · Jun 04, 2012 at 02:29 AM 0
Share

Such as, Jessy ?

avatar image DGArtistsInc · Jun 04, 2012 at 02:31 AM 0
Share

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.

avatar image Datael · Jun 04, 2012 at 03:59 AM 0
Share

@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.

avatar image fafase · Jun 04, 2012 at 06:27 AM 1
Share

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.

Show more comments
avatar image
0

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges