Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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
5
Question by spinaljack · Apr 26, 2010 at 07:21 PM · getcomponentsendmessage

What's the difference between SendMessage and GetComponent?

If for instance you were doing weapon damage you could use SendMessage("ApplyDamage",20) or you could use GetComponent(EnemyScript).ApplyDamage(20). One doesn't require any knowledge of what scripts are on the receiving end but apart from that what's difference? Which is faster? Is there a reason to use one instead of the other?

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
9
Best Answer

Answer by Squeegy · Apr 26, 2010 at 07:40 PM

I would assume that

  1. There is no functional difference as long as only one script implements that method.
  2. GetComponent would be faster since it does not have to look for method on all attached components.

I would use SendMessage when implementing a protocol of messages that many different scripts can optionally repsond to. This allows you to have a good deal of flexibility. You can simply tell an object to DoStuff without having to know what kind of object it is or what scripts are on it. It is left up to the object itself to know how to do it's own stuff.

But if enemies are the only things that you ApplyDamage to, it makes more sense to call it directly. Use the SendMessage route if you want to have a variety of scripts trigger when you ApplyDamage to an object.

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
5
Best Answer

Answer by dhendrix · Apr 26, 2010 at 07:42 PM

SendMessage is quite a bit slower than GetComponent, though using it a few times per frame is not so bad, and it is much more convenient. Generally you only have to worry about it on iPhone development. But either way, its usually best to design first, and optimize later.

This forum topic has more info on the subject, including time comparisons.

Comment
Add comment · Show 2 · 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 spinaljack · Apr 26, 2010 at 08:38 PM 0
Share

Thanks for the link

avatar image Molix · Apr 27, 2010 at 01:13 AM 0
Share

According to the post, GetComponent is probably as slow as Send$$anonymous$$essage; the performance gain comes from caching the component after a single call to GetComponent.

avatar image
5
Best Answer

Answer by Molix · Apr 26, 2010 at 08:05 PM

Make sure you read that link carefully! It states that GetComponent is "probably at least as slow as" SendMessage. The performance gain comes from caching the reference to the target (i.e. calling GetComponent once in Start()). e.g. (C#)

private EnemyScript enemy = null;

void Start() { enemy = GetComponent(typeof(EnemyScript)) as EnemyScript; }

void DoStuff() { enemy.ApplyDamage( 20 ); }

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

No one has followed this question yet.

Related Questions

how to make one script change a variable in another scipt 2 Answers

once again, Sendmessage VS Getcomponent! 1 Answer

more pragma strict getComponent and SendMessage problems 0 Answers

GetComponent for all enemies 1 Answer

pickup weapon with GetComponent 0 Answers


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