• 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 ensomniac · May 27, 2013 at 09:35 PM · javascriptglobal

How can I emit a global function call?

I'm looking to create a function that behaves similarly to the built in Unity functions Update(), Start(), Awake(), OnCollisionEnter(), etc.

I would like to place a function in several scripts and call all of them at once. For instance, let's say I want to create a touch tracking function that will emit something like "touch_started()" and "touch_ended".

I'd like to be able to add those functions to any number of scripts and have them get called from a different touch tracking script. I don't want to manually call all of the instances where they are defined.

Rather than:

 world_1_script.touch_started();
 world_2_script.touch_started();

I'd like to be able to do something like:

 *.touch_started(); 

(Obviously that's not going to work and isn't proper code. It's simply an example to illustrate my objective)

Any help would be greatly appreciated. Thanks

Comment
Add comment · Show 4
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 robertbu · May 27, 2013 at 09:36 PM 0
Share

If you are program$$anonymous$$g in C#, take a look at C# events and delegates.

avatar image ensomniac · May 27, 2013 at 09:38 PM 0
Share

Unfortunately I'm using JS

avatar image robertbu · May 27, 2013 at 11:24 PM 0
Share

You could find all the game objects or components and use Send$$anonymous$$essage() or Invoke(). It would be better to cache the references if you can.

http://answers.unity3d.com/questions/12103/is-it-possible-to-broadcast-to-all-objects-in-scen.html

avatar image Benproductions1 · Jun 03, 2013 at 04:18 AM 0
Share

Can this question be closed or an answer accepted?

2 Replies

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

Answer by ensomniac · Mar 03, 2014 at 10:06 PM

I saw that this question started getting lots of views so, despite it being a duplicate question, I'm going to post the proper answer I was looking for in case other people find it useful and don't feel like clicking and reading another question.

To globally call a function in any script, you actually use SendMessage(). Here is an example:

 gameObject.SendMessage("touchStarted", someValue);

That will cause every function named "touchStarted" in every script (C & JS) to fire:

 function touchStarted(someValue:float) {
     print(someValue);
 }

Pretty awesome.

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
0

Answer by Benproductions1 · May 28, 2013 at 04:41 AM

Duplicate Question!

http://answers.unity3d.com/questions/31377/sending-and-receiving-messages-globally-with-broad.html
http://answers.unity3d.com/questions/12103/is-it-possible-to-broadcast-to-all-objects-in-scen.html

Hope this helps,
Benproductions1

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 robertbu · May 28, 2013 at 05:48 AM 1
Share

@$$anonymous$$roductions1 - In all fairness, if you don't know what to call it, is is difficult to search for it. Googling for "How can I emit a global function call Unity3d" doesn't produce any hits for Send$$anonymous$$essage() or Broadcast$$anonymous$$essage().

avatar image Benproductions1 · May 28, 2013 at 10:38 AM 0
Share

@robertbu If people knew the diction, like you call a function, you don't "emit" it. Global is used in a different context etc... This would not have happened...
Where are people learning there words??? :O

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

14 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 avatar image avatar image

Related Questions

make network script connect over diferent wifi's 0 Answers

Is there any way to edit variables inside other scripts without declaring them as static? 1 Answer

Javascript : Get variable & run a method from another script 0 Answers

How do you achieve variables and functions that are global between scenes? What is the BEST way? 1 Answer

How to make a GLOBAL variable in javascript (Unity) 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