- Home /
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.
Answer by Matthew A · Jan 01, 2011 at 02:48 PM
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).
Answer by The_r0nin · Dec 27, 2010 at 09:07 PM
Put this on the ammo box (in a collision check):
collision.gameObject.SendMessage("addAmmo",ammo,SendMessageOptions.DontRequireReceiver);
if you read above that is what ive done however it is in the receiver where i have the problem
Answer by heks · Dec 27, 2010 at 11:41 PM
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");
}
Your answer
Follow this Question
Related Questions
SendMessage , how can i send two parameter 3 Answers
BroadcastMessage 1 Answer
Flame Thrower - Particle Collision Help 1 Answer
Pass Enum To Function Parameter 2 Answers
How do i Specify a Receiver for a Send Message Function? 2 Answers