Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
1
Question by zachypin · Jul 19, 2011 at 07:42 PM · sendmessagemessageoopbroadcastmessagebroadcast

BroadcastMessage Functionality Help

So I have two scripts, a JS and a CS.

My CS script has a function:

 static void ChangeVar(bool var)
 {
     if(var) 
       testVar = true;
 }

My JS script has a line that gets executed:

 object.BroadcastMessage("ChangeVar", true);

I get an error saying there is no receiver, not sure why.

The setup of these two scripts is: The JS is attached to an object that at the top has a line of code:

 public var object : GameObject;

the object that has the CS attached to it is on the gameobject that is put into "object". I figured this was enough for BroadcastMessage to work.

What am I doing wrong?

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

1 Reply

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

Answer by Alec-Slayden · Jul 19, 2011 at 08:01 PM

SendMessage affects all scripts on the calling game object. BroadcastMessage affects all scripts on the calling game object and its children. SendMessageUpwards affects all scripts on the calling game object and its ancestry.

If you want game object X to BroadcastMessage to game object Y, you'll need Y to be its child (or be its parent and use SendMessageUpwards).

Also, you don't need to use object, you can just BroadcastMessage("ChangeVar", true) and it will be caught by any scripts on the object or its children with that function.

By default, these messaging operations will throw an error if no one is listening to them. This error can be avoided with SendMessageOptions, like so:

 BroadcastMessage("ChangeVar", true, SendMessageOptions.DontRequireReceiver);

EDIT: per alternative

If your game is such that you cannot alter the hierarchy of the objects in question, you can refer directly to the script on the other object by name, and call the function directly, using GetComponent

for example, instead of BroadcastMessage, you could use:

 object.GetComponent("OtherScript").ChangeVar(true);

Where 'OtherScript' is the name of the actual changevar script, minus the extension. If you expect to use this component again later, it is advised that you store object.GetComponent("OtherScript") in a variable for quick use.

Comment
Add comment · Show 5 · 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 zachypin · Jul 19, 2011 at 11:00 PM 0
Share

these are two separate objects. this is one of the ways I was thinking of doing it, is this not possible? if not then check this link out because it's the other way I was thinking of doing it. http://answers.unity3d.com/questions/145790/push-gui-button-change-another-objects-variable.html?viewedQuestions=137913&viewedQuestions=136227&viewedQuestions=136247

avatar image Alec-Slayden · Jul 19, 2011 at 11:17 PM 0
Share

$$anonymous$$ultiple objects are fine and expected with Broadcast$$anonymous$$essage. What I tried to express was that If you're using broadcast message, make the object you are broadcasting to a child of the one you are broadcasting from. Static variable use, or references to other object components, are also viable methods if you give up on broadcasting.

avatar image zachypin · Jul 19, 2011 at 11:30 PM 0
Share

yes but what if i can't make that object a child of it? there must still be a way for them to communicate. i have a button part of an object that needs to affect a completely different object that has its own dependencies

avatar image Alec-Slayden · Jul 19, 2011 at 11:39 PM 0
Share

there are several ways; I'll edit with the first that comes to $$anonymous$$d

avatar image zachypin · Jul 21, 2011 at 04:12 PM 0
Share

That's exactly what I was looking for! Thanks!

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Send messages to different gameobjects 1 Answer

BroadcastMessage Null Reference Exception Problem 1 Answer

BroadcastMessage between children? 3 Answers

Passing 'null' as parameter to SendMessage/BroadcastMessage 2 Answers

BroadcastMessage from one Object to another ? 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