x


How to Send Message to other GameObject

how do you SendMessage to another object?

i am trying to send addAmmo from a box on collider enter with the Player,

for some reason i can only recieve the message on scripts attached to the player i want to go to my Gun.

var ammo = 20;

function OnTriggerEnter (collision : Collider) {

if (collision.gameObject.FindWithTag ("Player"))
SendMessage("addAmmo",ammo,SendMessageOptions.DontRequireReceiver);
    Debug.Log("sent");
    DestroyObject (gameObject);
}

i get the Debug |Log so its workin'...

function addAmmo (ammo : float) {

Ammo += ammo;
Debug.Log("PickUP");

}

however this dosent.

more ▼

asked Dec 27 '10 at 09:01 PM

Mike Ledwards gravatar image

Mike Ledwards
212 26 29 38

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

3 answers: sort voted first

Do you mean that the message is sent only to the Player object, and you need it to propagate down the object hierarchy to your gun object???

If so, you want to use BroadcastMessage instead of SendMessage (with the same parameters).

more ▼

answered Jan 01 '11 at 02:48 PM

Matthew A gravatar image

Matthew A
517 7 9 20

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

Since you're assigning an integer to your ammo variable I suspect the addAmmo() function should take an integer as well (instead of float):

function addAmmo (ammo : int) {
    Ammo += ammo;
    Debug.Log("PickUP");
}
more ▼

answered Dec 27 '10 at 11:41 PM

heks gravatar image

heks
76 3

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

Put this on the ammo box (in a collision check):

collision.gameObject.SendMessage("addAmmo",ammo,SendMessageOptions.DontRequireReceiver);
more ▼

answered Dec 27 '10 at 09:07 PM

The_r0nin gravatar image

The_r0nin
1.4k 10 14 30

if you read above that is what ive done however it is in the receiver where i have the problem

Dec 27 '10 at 09:10 PM Mike Ledwards
(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:

x336
x84
x68
x36

asked: Dec 27 '10 at 09:01 PM

Seen: 5940 times

Last Updated: Dec 27 '10 at 09:11 PM