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 SepM · Jul 07, 2014 at 02:05 PM · beginnergetcomponentfunctionsendmessagefighting

Send Message?

I want to activate a function of of one script with another. I've been told to search up at GetComponent and SendMessage but it's not making any sense to me (didn't help very much -_-). I'm still sort of a beginner, so can someone give me a simple explanation of how it works?

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 meat5000 ♦ · Jul 07, 2014 at 02:02 PM 1
Share

Try these links, to see if they help.

http://docs.unity3d.com/412/Documentation/ScriptReference/index.Accessing_Other_Game_Objects.html

http://answers.unity3d.com/questions/313568/why-do-i-have-to-use-getcomponent-function.html

http://www.unity3dstudent.com/2011/02/beginner-b28-sendmessage-to-call-external-functions/

http://unity3d.com/learn/tutorials/modules/beginner/scripting/getcomponent

http://answers.unity3d.com/questions/7555/how-do-i-call-a-function-in-another-gameobjects-sc.html

avatar image SepM · Jul 07, 2014 at 05:29 PM 0
Share

I was asking for a nice, short, and concise explanation, but I'll check these out anyway...

UPDATE: Not working out for me. It's not making sense.

avatar image roojerry · Jul 07, 2014 at 07:03 PM 1
Share

You sure you did the Unity Tutorial on GetComponent linked above? That should be about as simple an explanation as possible to get you started, other than someone co$$anonymous$$g to your house and $$anonymous$$ching you to how to write the code. If you weren't able to understand some of the concepts in that tutorial, you probably need to start from the beginning of the Unity Scripting Tutorials or/and the Unity Editor Tutorials

1 Reply

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

Answer by Simba · Jul 07, 2014 at 07:06 PM

SendMessage invokes a function on every script on the game object you call it on. For example, if you say:

 gameObject.SendMessage("Foo");

And, in another (or the same) script, on the same gameobject, you have:

 function Foo () {
     print("Hello");
 }

Then your call to SendMessage will run the function Foo.

The downside of this is that if you have multiple instances of the same script, it will run the same for all of them, because that's what SendMessage does. It's great for, say, when you have six guns, all with their own script on the same object, and you want to call Fire() at the same time on all of them. But it's not great if you want to fire just one gun. That's this method comes in:

 // Your script where you want to call the function
 var otherScript : NewScript;
 
 function Start () {
     otherScript = gameObject.GetComponent(NewScript); // Use GetComponent to find the script.
     //If you delete the above line you can drag a NewScript to this script's otherScript variable in the Inspector window, which is useful
     
     otherScript.Foo(); // This runs Foo() on the otherScript
 }
 
 // NewScript.js
 function Foo () {
      print("Hello");
 }

(Note: Untested code, NewScript might be a reserved name, but you get the idea) Hope this helps.

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 SepM · Jul 09, 2014 at 11:55 PM 0
Share

Thank you! It works well!

avatar image meat5000 ♦ · Jul 10, 2014 at 12:54 AM 0
Share

Ah yes...the Copy and pastable version...

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Unity Touch=2 commends? 1 Answer

NullReference when calling a function to another script 2 Answers

Message sender...how to know who send the message 1 Answer

Accessing Multiple GameObjects' Script Functions 1 Answer

Move a cube from GUITexture. Is not working. Where is my mistake 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