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 Patyrn · Mar 07, 2011 at 09:27 PM · componentsendmessage

SendMessage or store reference and call directly?

If an object may or may not have a certain component, is it best to attempt to store a reference to it, or simply use SendMessage without requiring a receiver?

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Jessy · Mar 07, 2011 at 09:54 PM

"Best" sounds like a subject for the forum, but here's my take:

The best thing to do is to not do things to other objects directly. The best thing to do is to have those other objects listen for events, and check for null before firing those events. (The check for null isn't necessary if you are certain that something is going to be listening for the event, but that doesn't sound like a description of your situation.)

http://www.youtube.com/watch?v=N2zdwKIsXJs

Comment
Add comment · Show 3 · 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 Patyrn · Mar 07, 2011 at 10:03 PM 0
Share

I'll have to check that video out after work. When I say "best" I mostly mean a good tradeoff between performance and ease-of-use and code readability. An example of how I would use it is a projectile sends a "Take$$anonymous$$ineticDamage" to whatever it hits. That just seems to make sense to me, but I don't want to do it if it's slow with 50+ projectiles.

avatar image Patyrn · Mar 07, 2011 at 11:13 PM 0
Share

Having watched that video it would seem that is an excellent alternative to Broadcast$$anonymous$$essage, but what about events targeted at a specific thing? Should I do gameObject.GetComponent("HitDetector").Take$$anonymous$$ineticDamage() or gameObject.Send$$anonymous$$essage("Take$$anonymous$$ineticDamage");

avatar image Jessy · Mar 08, 2011 at 12:06 AM 0
Share

I need to know more about your scene. It sounds to me, like what I would do, is have the thing being hit get information about the projectile that hit it, and be damaged appropriately. Events aren't a good match when you have to perform checks in each object to see if they are the thing that should get affected, so I don't recommend them, now. I thought you were talking about a single object.

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

No one has followed this question yet.

Related Questions

BroadcastMessage frustratng problem 1 Answer

2D Animation does not start 1 Answer

"Send Message Has No Receiver" On Button Click 1 Answer

where is the difference? (js-scripting) 1 Answer

Unity 2018.3.8f1 HDRP : Area Light Shadows feature not showing for Area and Tube Lights 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