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 AyJayKaySoft · Sep 19, 2012 at 02:03 PM · javascriptcomponentsendmessage

where is the difference? (js-scripting)

Hi there :) I got two questions but I think they are in the same 'main question'... where is the difference between GameObject.SendMessage(FunctionName) and GameObject.FunctionName? and between GameObject.Component and GameObject.GetComponent(Component)? So, the only thing I know is, that you aren't able to call all components and in no way scripts with the shorter way. Is GameObject.Component only shorter for often used Components? thanks ;)

Comment
Add comment · Show 2
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 Lockstep · Sep 19, 2012 at 03:55 PM 0
Share

I don't know for sure myself so this might not be 100% correct. Send$$anonymous$$essage addresses any script of your gameObject. If you have two scrips say enemyHealth.js and playerHealth.js and both have the function ApplyDamage( dmg: float). If you use Send$$anonymous$$essage("ApplyDamage", 10) ApplyDamage will be called no matter which script your gameObject has. If you used gameObject.enemyHealth.ApplyDamage(10) you get a nullreference if the gameObject has the playerHealth script. However calling a function directly is cheaper than Send$$anonymous$$essage since Send$$anonymous$$essage will look in all scripts until it found the function ApplyDamage.

For the GetComponent I have no idea myself. I usually use GetComponent to define a shortcut variable like :

var target : Transform;

var targetHealth : enemyHealth;

function Start(){

targetHealth = target.GetComponent(enemyHealth);

}

Note that there also is the GetComponents[] GetComponents<>; method which will return all specified components of your gameObject.

avatar image by0log1c · Sep 19, 2012 at 04:06 PM 0
Share

The short answer is that GetComponent retrieve the actual object and 'directly call' the desired method, Send$$anonymous$$essage use reflection over all scripts and invoke all methods with the given name. A direct call (GetComponent) is much more efficient and that approach is to be favored where possible, imo.

1 Reply

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

Answer by SarperS · Sep 19, 2012 at 05:09 PM

SendMessage calls the method you give in the parameter on every script of the gameobject, GameObject.FunctionName points to a method in GameObject class, the methods you can call that way are coded into the GameObject classs by Unity.

GameObject.Component is the same, there are some built in properties in GameObject class, referencing the rigidbody, collider etc. with GetComponent() method you can find other components and your custom scripts.

So in short, GameObject.GetComponent() or GameObject.transform are internal (not to be confused by the C# access modifier) methods and properties of the GameObject class but GameObject.GetComponent() returns your custom components you add to the gameobject.

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

12 People are following this question.

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

Related Questions

SOLVED: Problem with SendMessage -- silently failing 2 Answers

RayCast to send message to the object it hits? 1 Answer

Web Player SendMessage Array Parameters 1 Answer

Problem with UnityObject2.SendMessage | communicating with the web player 0 Answers

SendMessage Not Working: JavaScript 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