Health script not functioning. Broadcast message difficulties arise.

Let me start out by saying that, yes I am (relatively)new to Unity. I’ve used it for years, but I still have yet to achieve something like “mastery”. But to the topic at hand.

I have been having severe difficulty with the “BroadcastMessage” function in a health pick up script. As I have stated previously, I am new to unity and unity code, so I made the script using existing instances of “BroadcastMessage” such as this excerpt of a “rayshoot” script were the message “applydamage is sent”

  • note * script has been modified, but not originally created by me. A Hummad Nazir or Hyper Shade Tutorials is the creator (as far as I know).

      function RayShoot (){
    

    GameObject.Find(“Riffle”).animation.Play(“ARshoot”); //(if your player has animations then remove the // infront of the GameObject.Find and then
    //(replace your objetcs name there. and then the animation name)
    var hit : RaycastHit;

      			 var direction : Vector3  = transform.TransformDirection( Vector3.forward);
           
      			Debug.DrawRay(transform.position , direction * Range , Color.blue);
      			
      			if(Physics.Raycast(transform.position , direction , hit, Range)){
      			var hitRotation = Quaternion.FromToRotation(Vector3.up, hit.normal);
      			
      			if(hit.rigidbody){
      			
      			if( hitParticles){
      			
      			hitParticles.transform.position = hit.point;
      			
      			hitParticles.transform.localRotation = Quaternion.FromToRotation(Vector3.up, hit.normal);
      			hitParticles.Emit();
    
      		 			  
      		    hit.rigidbody.AddForceAtPosition( direction * Force , hit.point);
      		    hit.collider.SendMessageUpwards("ApplyDamage" , Damage , SendMessageOptions.DontRequireReceiver);
      			}
      			}
    

And here, the script to receive said damage:

var MaxHealth : int = 100;
var CurrentHealth : int;
var dead: Transform;
var deadNewPos: Transform;


function Start () {

CurrentHeath = MaxHealth;

}

function ApplyDamage ( Damage : float ) {

if( CurrentHealth < 0){

return;

}

CurrentHealth -= Damage;

if( CurrentHealth <= 0 ){

	dead = Instantiate(deadNewPos, dead.transform.position, dead.transform.rotation);
	Destroy( gameObject) ;

}

}

Using these instances of “MessageSending” (Although it’s not broadcasting, which may be my problem) I took a look into my two health scripts:
A script for the Player’s desired “Health Kit” and the Players own “Damage2” script.

Once touched by the Player the health kit should: make as sound, broadcst the “heal message” and “give the Player health” and, delete itself.

public var Player = GameObject;

public var Healing : int = 20;
public var PickUpAudio : AudioClip;

function OnCollisionEnter(trigCollision : Collider){
    if(trigCollision.transform.tag == "Player" && transform.collider.isTrigger){    
        PlayPickUpAudio();

		Player.BroadcastMessage ("ApplyHealing", 20 , SendMessageOptions.DontRequireReceiver);
        yield WaitForSeconds(0.25);
        Destroy( gameObject) ;
    }
}
function PlayPickUpAudio(){

  audio.PlayOneShot(PickUpAudio);


}

And here is the player, Damage2 scr-… well due to my “modification” this script is rather messy in my opinion, but it’s still (somewhat) easy to make sense of.

var curHealth : int = 100;
var maxHealth : int = 100;
var healthtext : GUIText;



function Start () {


healthDegen();

}

function ApplyHealing ( Healing : float ) {


return;
}

curHealth += Healing;

function OnCollisionEnter(col : Collision){

    if(col.gameObject.tag == "Enemy")
    {
        curHealth -= 20;
        
    }
}

//function Heal(trigCollision : Collision){

//    if(trigCollision.transform.tag == "HealthKit")
 //   {
//        curHealth -= 20;
//    }
//}




function Update () {




healthtext.text = curHealth + "";


if(curHealth < 0 ) {

curHealth = 0;


}


if(curHealth > 100) {


curHealth += 100;


}


}




function healthDegen () {


for(i=1;i>0;i++) {



yield WaitForSeconds(0.5);

if(curHealth > maxHealth) {

curHealth--;

}

if( curHealth == 0 ){

transform.position.x = 2;
		transform.position.x = 61.5;
		transform.position.y = 5.597397;
		transform.position.z = -38;
		transform.rotation.x = 0;
		transform.rotation.y = 0;
		transform.rotation.z = 0;
		
		curHealth = 100;


}



}

}

It’s late the at the time of me posting this (And I mean REALLY late) so I’ll wrap this up.

I’m getting the compiler error “Unknown Identifier” for the Healing function in the “Damage2” script eluding to the idea that broadcast message is not working in the kit script. Whether it’s spelling or bad use of variables I cannot tell at the moment.

I KNOW I messed up somewhere along the way and I desperately desperately need some guidance here.

I feel like I’m leaving info out but I can’t stay awake another moment (It’s late I say again) please leave comments, ask questions and, please don’t tear me apart…

We were all noobs at one point or another, and that cannot be denied.

  1. please fix your formatting. use the 101/010 code tag button to highlight all of the code. you got most of it… code that’s easier to read is generally easier to spot mistakes in… ymmv.

  2. if you’re getting an error, i’ll go out on a limb here and guess that maybe there was some line number mentioned. that might be pertinent information. just sayin’

  3. line 20 contains this:

    curHealth += Healing;

not enclosed in any function…

  1. maybe it’s supposed to be part of the preceding ApplyHealing() function which currently does nothing.

maybe when you’ve slept things will be clearer.