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
4
Question by FreeTimeDev · Oct 09, 2010 at 09:48 AM · sendmessagealternative

Alternative to SendMessage

First, let me say that I wouldn't ask without doing some research myself. However, there's nothing specific (or at least enough so that I would understand) on the matter.

At first, I was trying to implement a SendMessage system so that "Spell" could kill the "Enemy".

Sendmessage("Function", #)

And then on the Enemy:

function Function(var : int)
...bunch of ifs

Basically, depending on the element of the Spell the Enemy will react a certain way.

While trying to implement SendMessage (I was having issues referencing the clone, and not the prefab) I've come across several people recommending avoiding SendMessage altogether for a better alternative.

In this scenario, what's the better alternative?

Thank you in advance for your wisdom.

Comment
Add comment · Show 3
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 Lenn Dolling · Jan 21, 2012 at 06:12 AM 0
Share

this link http://unity3d.com/support/resources/unite-presentations/techniques-for-making-reusable-code edges to using an alternative to sendmessage... I say use whatever it takes to get the job done. :)

avatar image Kryptos · Jan 21, 2012 at 12:17 PM 0
Share

Ever heard of delegates and events in C#?

avatar image Lenn Dolling · Jan 22, 2012 at 02:02 AM 1
Share

these links should help out as well. http://www.41post.com/3146/program$$anonymous$$g/using-c-delegates-in-unity3d-scripts ohh and
http://www.unifycommunity.com/wiki/index.php?title=CSharp$$anonymous$$essenger_Extended

3 Replies

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

Answer by Ray-Pendergraph · Oct 09, 2010 at 11:40 AM

One alternative I have used to SendMessage is to have a class (or an abstract class) that extends MonoBehaviour that basically describes the behavior and properties that encapsulates those related facets of the entity and has the method you want. Then use the GetComponent<> method or GetComponentInChildren<>. If it's not found then you don't bother sending the "message".

For example, if I wanted a GameObject to be able to be hit and damaged in the game I would define a class (or abstract if there were going to be multiple strategies) maybe called 'Hittable' or something that had methods like 'public void TakeHit(WeaponType type, int force)' and properties like 'public float HitPointOffset' and other things like these.

The advantages of this approach are that it requires a much stronger (compile time) contract between the sender and receiver than SendMessage even if you specify that a recipient is required. Also, I suspect that SendMessage uses reflection to check for the method on each MonoBehaviour (and invokes it) so from that perspective being able to do a selective broadcast by GetComponentInChildren<>() and then calling your method on those found may buy you a little efficiency. I wouldn't read to much into the efficiency thing though as I think Unity uses the SendMessage approach a whole lot internally. I am not sure that this is 'better' but I like it because of the strong contract and easy refactoring.

Hope some of that helps.

Comment
Add comment · Show 4 · 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 Peter G · Jan 21, 2012 at 03:11 PM 2
Share

An interface would probably make more sense here. That way you would have to base your inheritance chains on a single class

avatar image Kryptos · Jan 21, 2012 at 04:34 PM 0
Share

An interface would not work as GetComponent can only retrieve object derived from Component (thus class or abstract class).

avatar image Peter G · Jan 23, 2012 at 02:26 AM 0
Share

You can always check:

     class.IsAssignableFrom()
avatar image Bunny83 · Jan 23, 2012 at 03:57 AM 0
Share

GetComponent does work with interfaces, but not the generic version because the generic parameter has a constraint to Component. The System.Type version work pretty well with interfaces. The generic version is just a wrapper of the System.Type version with an "as-cast".

However, if the OP uses UnityScript interfaces are kinda usless since you can derive / "extend" only from one type in Unityscript. C# works fine. It's one of the few differences between the languages.

avatar image
4

Answer by Eric5h5 · Oct 09, 2010 at 11:01 AM

Nobody should recommend avoiding SendMessage for a "better" alternative. Calling functions directly is faster than SendMessage, but that doesn't necessarily make it better. If SendMessage is appropriate for what you're doing and you don't need to call it a huge number of times then there's no reason to avoid it.

Comment
Add comment · Show 1 · 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 GVGiu · Nov 22, 2012 at 11:23 AM 0
Share

I have a similar doubt, on the side of performance, what is better:

  • Call Send$$anonymous$$essage on a $$anonymous$$onoBehaviour

    
    $$anonymous$$yScript.Send$$anonymous$$essage("$$anonymous$$ethod", params);
    

  • Cast the $$anonymous$$onoBehaviour to a defined interface

    
        I$$anonymous$$ethod script = $$anonymous$$yScript as I$$anonymous$$ethod;
    if (script != null)
    script.$$anonymous$$ethod(params);

Thought I suspect that in this example if I can cache the cast script it most probably will be better calling via interface, right?

avatar image
1

Answer by digital-synapse · Dec 09, 2017 at 04:23 AM

Yeah as previously stated, dont use SendMessage. Use GetComponent<> with interfaces. You get type safety way more speed. Since you are calling typed methods through an interface you can also have listeners respond back with results. Take a look at my ComponentBase.cs which handles getting all the listeners and adds way more granular control to scoping for GetComponent. https://gist.github.com/digital-synapse/12bc70e494067b85564a2039d184b65c

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

Alternative route 1 Answer

How does SendMessage work in the background? 3 Answers

Suggested workaround for SendMessage bug? 3 Answers

Magnetic Gameobject 1 Answer

Send Message Overload 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