x


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?

more ▼

asked Jul 27 '11 at 10:48 AM

DorkNemesis gravatar image

DorkNemesis
46 5 5 8

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

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.

more ▼

answered Jul 27 '11 at 01:26 PM

CHPedersen gravatar image

CHPedersen
6k 13 22 61

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 :)

Jul 27 '11 at 02:09 PM DorkNemesis
(comments are locked)
10|3000 characters needed characters left

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.

more ▼

answered Jul 27 '11 at 01:30 PM

Usul gravatar image

Usul
452 3 5 12

Caching script instances has only a minor 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.

Jul 27 '11 at 01:43 PM CHPedersen

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

Jul 27 '11 at 02:21 PM Usul
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x292
x184

asked: Jul 27 '11 at 10:48 AM

Seen: 1301 times

Last Updated: Jul 27 '11 at 02:21 PM