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
0
Question by DorkNemesis · Jul 27, 2011 at 10:48 AM · multiplesendmessage

SendMessage to multiple objects

Hey guys,

Quick question: I've designed something like a World of Warcraft-styled hotbar for my game. The way it works is that it's just 10 planes that should show the texture of the item that's put in that slot.

So the way I set this up is that I have a 'refreshTextures' function that just goes through stuff like:

if(item == "Potion"){ renderer.material.mainTexture = GameObject.Find("inventory").GetComponent(inventory).Potion;}

Now, all of this works just nicely. The problem is that I have 10 planes and the that function has to be called 10 times (for each plane). So what I want to do is:

If Player picks up Item, assign texture to hotbar slot(I already set that up) and then refresh all the hotbar textures.

Every single hotbar slot should receive the 'refreshTextures' message from the pickup, but every time I do it only the LAST hotbarSlot with the name, tag, variable, etc. actually changes its texture.

Isn't there a simple, non-convoluted way of sending out a message to MULTIPLE objects that then call a function at the same time?

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

2 Replies

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

Answer by CHPedersen · Jul 27, 2011 at 01:26 PM

There is - Maybe you want something like BroadcastMessage?

http://unity3d.com/support/documentation/ScriptReference/Component.BroadcastMessage.html

It calls the method whose name you supply with the argument you supply on the target object itself and all of its children, so whether it would work for you depends a little on how you have your gameobjects parented in your project.

You could definitely use it if your hotbarSlots are independent GameObjects, which are all children of the same parent GameObject (Hotbar). Then, you'd call BroadcastMessage on the Hotbar's script, and it should execute refreshTextures on all of its child hotbarSlots.

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 DorkNemesis · Jul 27, 2011 at 02:09 PM 0
Share

Thanks, that did it. I just re-parented a couple of things, do direct references on stuff and then broadcast the message to the parent. Seems to be fast enough :)

avatar image
0

Answer by PaulUsul · Jul 27, 2011 at 01:30 PM

You should read up on OOP(Object-oriented programming), it's a way of thinking and writing code that solves problems just like this one!

To the question, you could hold your items in an array and loop over them. It's also a lot faster then GameObject.Find and SendMessage.

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 CHPedersen · Jul 27, 2011 at 01:43 PM 0
Share

Caching script instances has only a $$anonymous$$or impact in this case, because he's not continually calling his refreshTextures-method. It only happens when the player picks something up. Broadcasting is fine in that case.

avatar image PaulUsul · Jul 27, 2011 at 02:21 PM 0
Share

So true. I still wouldn't use Broadcast even though it fits the problem. Because the lack of type safety and extensibility.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

SendMessageUpwards - Send Multiple Parameters? 3 Answers

Accessing an Array in another function 1 Answer

SendMessage with multiple parameters 3 Answers

Multiple rotations with Quaternion Slerp problem 0 Answers

multiple paths 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