- Home /
Sending Messages to Other game Objects?
I have a gun that shoots at a box which is the enemy and I need that to send a message to my player and gun so that the exp for the mastery of the gun and the exp for the player are both increased. How would I do this? I have tried putting a Broadcast message on bullet prefab so when it contacts the box the box dies and its sends out the message but it says that the message has no receiver. I think it is because the gun is not a child of the cube so send and broadcast message will not work. Can someone point me in the direction of a tutorial or a script that can help me send messages between different gameObjects and not just to childs of a gameObject. All help is appreciated, Thank you. Here is the script for the bullet:
var target : GameObject;
function OnCollisionEnter(collision : Collision) { if(collision.gameObject.tag == "enemybox") { Destroy(collision.gameObject); // Destroys Health Box target.BroadcastMessage("addExp"); } }
and the script for the gun where the exp is handled is:
var gunHealth : float = 100; private var maxgunHealth : float = 100; private var brokenHealth : float = 30; private var destroyed : float = 0; var broken = false; var unusable = false; //ammo debugging var ammo : int = 10; var maxammo : int = 10; private var emptyammo : int = 0; var clipammo : int; private var noclipsammo : int = 0; var empty = false; var reloading = false; var noClips = false; //exp var masteryExp : float = 0; private var leastmasteryExp : float = 0; var maxmasteryExp : float = 100; var levelofMastery : int = 1; var maxlevelofmastery : int = 10; var master = false; var damage : int = 5; //GUI var barDisplay : float = 0; var pos : Vector2 = new Vector2(20,40); var size: Vector2 = new Vector2(Screen.width / 2 ,20); var posoftext : Vector2 = new Vector2(20,40); var sizeoftext : Vector2 = new Vector2(Screen.width / 2 ,20); var progressBarEmpty : Texture2D; var progressBarFull : Texture2D; private var dead = false; var myskin : GUISkin; var mystyle : GUIStyle; function Start(){ //need to add the reloading animation animation["trigger"].wrapMode = WrapMode.Once; animation["trigger"].layer = 1; animation.Stop(); }
function Update (){
if (Input.GetMouseButtonUp(0)){
animation.Play("trigger");
}
if (Input.GetMouseButtonUp(0))
gunHealth -= .5;
if (Input.GetMouseButtonUp(0))
ammo -= 1;
if (gunHealth <= brokenHealth){
broken = true;
}
if (gunHealth <= destroyed){
unusable = true;
gunHealth = destroyed;
}
if (gunHealth >= maxgunHealth){
gunHealth = maxgunHealth;
}
if (ammo >= maxammo){
ammo = maxammo;
}
if (ammo <= emptyammo){
ammo = emptyammo;
empty = true;
clipammo -= 1;
}
if (clipammo <= noclipsammo){
clipammo = noclipsammo;
noClips = true;
}
if (masteryExp <= leastmasteryExp){
masteryExp = leastmasteryExp;
}
if (masteryExp >= maxmasteryExp){
levelofMastery += 1;
masteryExp = 0;
maxmasteryExp += 50;
damage += 3.5;
}
if (levelofMastery >= maxlevelofmastery){
levelofMastery = maxlevelofmastery;
masteryExp = 0;
master = true;
}
barDisplay = masteryExp;
}
function LateUpdate(){ if (broken){ Debug.Log("item is now broken. Get repaired soon"); //I need a GUI message here also that says what is said above. } if (unusable){ Debug.Log("item is now unusable"); Destroy(gameObject); //this is where I need the GUI message to go } if (empty){ Debug.Log("start reloading"); reloading = true; } if (reloading){ Debug.Log("reloading now"); ammo = 10; empty = false; reloading = false; } if (noClips){ Debug.Log("no more ammo available"); ammo = 0; //I need the gun shooting script to stop and I need it to play a clicking sound here when they try and shoot } if (master){ Debug.Log("you are now a master with this weapon"); damage = 35; }
}
function OnGUI() { GUI.skin = null; // draw the background: GUI.BeginGroup (new Rect (pos.x, pos.y, size.x, size.y)); GUI.Box (Rect (0,0, size.x, size.y),progressBarEmpty); GUI.skin = myskin; // draw the filled-in part: GUI.BeginGroup (new Rect (0, 0, size.x * (masteryExp / maxmasteryExp), size.y)); GUI.Box (Rect (0,0, size.x, size.y),progressBarFull); GUI.EndGroup ();
GUI.EndGroup ();
GUI.Label(Rect(posoftext.x,posoftext.y,sizeoftext.x,sizeoftext.y), "Level Of Mastery " + levelofMastery, mystyle);
} function addgunExp(){ masteryExp += 10; }
The exp system works the same for both the gun and the player so I just need help sending the message to the indivisual game Objects Thank you.
Answer by Mike 3 · Mar 22, 2011 at 06:46 AM
If you have set the gun as target, you're nearly there - you have a spelling mistake in the BroadcastMessage
Your function is addgunExp, but you're trying to call addExp
Also - if you want to send a message to just the one gameobject, use SendMessage
I already tried what your saying it still says in the debug addExp has no receiver with both send and Broadcast message the gun is a child of the main camera which is a child of the player game object. Does this change anything of what I can do?
O$$anonymous$$ you kinda helped with the send message thing but I found out what the true problem was it was trying to send a message to another prefab not to a game Object in the heirarchy lol I fixed it and now it works fine thank you for your help
Your answer
