• 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
8
Question by Jaap Kreijkamp · Feb 25, 2010 at 05:24 AM · broadcastmessage

Is it possible to broadcast to all objects in scene?

Is there a way to send a broadcast to all gameobjects in a scene (without requesting all gameobjects with findObjectsOfType and send it to them individually)?

It would be good enough if there's a way to iterate through the root GameObjects in the scene, then I could just use Broadcast on these gameobjects.

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

7 Replies

· Add your reply
  • Sort: 
avatar image
15

Answer by Jaap Kreijkamp · Feb 26, 2010 at 09:53 AM

It seems there isn't, so as performance isn't a real issue for me as I only use to call it when starting and ending a level, this is my solution:

public static void BroadcastAll(string fun, System.Object msg) {
    GameObject[] gos = (GameObject[])GameObject.FindObjectsOfType(typeof(GameObject));
    foreach (GameObject go in gos) {
        if (go && go.transform.parent == null) {
            go.gameObject.BroadcastMessage(fun, msg, SendMessageOptions.DontRequireReceiver);
        }
    }
}

Don't know if I win anything with broadcasting to root objects instead of just use SendMessage on all gameobjects returned, but it works.

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

Answer by Motionreactor · Feb 26, 2010 at 03:36 PM

Really simple solution is to make everything in your scene a child of a single GameObject (call it world if you like). Then from this 'world' GameObject you can broadcast to all children:

gameObject.BroadcastMessage("ReceiverFunction", msg)

and place a simple script in every child you wish to receive the message:

function ReceiverFunction(msg)
{
    //do something with msg
}
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 Jaap Kreijkamp · Feb 27, 2010 at 03:26 AM 0
Share

Yeah a good one, but really, I don't want the performance hit of an extra matrix multiply of an extra level in hierarchy depth for a broadcast I do only twice in a level. I don't know how much impact the extra level would have but the transformation and scale would probably not be free, if it's zero and identity.

avatar image Motionreactor · Feb 27, 2010 at 05:06 AM 0
Share

I wouldn't imagine it'd be a large overhead. I would assume that the efficiency gained through simpler message broadcast would offset the loss in matrix addition (which would surely be $$anonymous$$iscule anyway?) Or am I wrong? (little experience to go on there)

avatar image Fattie · Mar 08, 2015 at 05:41 AM 0
Share

the overhead is nothing. but it's definitely true it's not an elegant, always-usable solution... (note -- apart from anything else, in 100% of projects you have persistent objects that come in an out of the scene for various reasons .. they would "miss out" in this scheme you know?)

avatar image
2

Answer by andeeee · Feb 25, 2010 at 05:05 PM

There isn't a way to do this directly. The best thing I can think of is to add a tag to all root objects. You could then get them all with FindGameObjectsWithTag and broadcast to each of them.

Comment
Add comment · Show 1 · 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 Jaap Kreijkamp · Feb 26, 2010 at 09:49 AM 2
Share

Except I use tags already for other purpose, but thanks.

avatar image
2

Answer by $$anonymous$$ · Nov 27, 2015 at 09:30 AM

you should use events instead https://unity3d.com/learn/tutorials/modules/intermediate/scripting/events

Comment
Add comment · Show 1 · 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 WarpZone · Mar 03, 2018 at 09:17 PM -1
Share

If you're making a complex system to sell on the appstore with lots of separate scripts talking to each other, DON'T USE EVENTS! Ins$$anonymous$$d of spaghetti, it turns your code into teleporting spaghetti!

avatar image
0

Answer by insominx · Mar 22, 2012 at 08:42 AM

You could always climb the tree to get the root and broadcast from there. This has worked for me:

 Transform root = this.transform;
 while (root.parent != null) {
     root = root.parent;
 }

 root.BroadcastMessage("MyMessage");
Comment
Add comment · Show 4 · 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 Chirokidz · Aug 23, 2013 at 02:52 AM 3
Share

there is a transform.root property for this purpose. Also, that's not the question, the question is sending the message to ALL root objects, not just the root of the current object.

avatar image JiMMaR Chirokidz · Feb 21, 2016 at 04:30 PM 0
Share

I didn't know about the root property ! thanks a bunch !

avatar image insominx · Aug 29, 2013 at 11:36 PM 0
Share

I didn't know about root. Has that always been there? As for the root of this object, all paths lead to the root level. It doesn't matter which one you start from.

avatar image Lohoris2 · Dec 19, 2014 at 09:26 AM 0
Share

You totally misunderstand how root works: http://docs.unity3d.com/ScriptReference/Transform-root.html

  • 1
  • 2
  • ›

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

7 People are following this question.

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

Related Questions

broadcastmessage Porblems 1 Answer

Can I only pass one parameter to BroadcastMessage? 2 Answers

Using BroadcastMessage on OnCollisionEnter 0 Answers

Send messages to different gameobjects 1 Answer

How can I Broadcast a message on a GameObject and also send the message Upwards natively without sending twice the message on the target base object? 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