Cant create damage script

Hey guys, I was creating a game in unity, a zombie-shooter kind of project, and can’t seem to get the script for zombies damaging the player to work. I use the projectile script as the zombie damaging the player, and the health script as the player’s health. Any ideas/tutorials?

3933559–336202–Health.cs (2.42 KB)
3933559–336205–Projectile.cs (6.02 KB)

To be honest I never knew GameObject.SendMessageUpwards() even existed. I would instead reference the Health script directly like public Health health; and then do health.ChangeHealth() within the projectile script.

EDIT: Actually, looking at it again, I suppose your attaching Health.cs to the player game object?

Would it maybe be a better idea to do a nested class like:

//somewhere you'd do like:
// ********************

//Script #1

MY MONODEVELOP SCRIPT (Your Scene Character Manager script) OR WHATEVER:

using RPG;

List<Character> scene_Characters = new List<Character>();

Start() {
 
}

public void AddCharacter(int ID, string setName, GameObject associatedGameObject, int setMaxHP) {

   Character newCharacter= new Character();
   newCharacter.ID = setID;
   newCharacter.Name = setName;
   newCharacter.gameObject = associatedGameObject;
   newCharacter.maxHP = setMaxHP;

   scene_Characters.Add(newCharacter);

   newCharacter.Initialize();
}

public void RemoveCharacter(string characterName) {
   foreach (Character character in scene_Characters) {
     if (character.Name == characterName) {
         scene_Characters.Remove(character);
        return;
     }
   }

   Debug.LogError("Couldn't find character with name of ''" + characterName + "'' in scene_Characters list!");

}

Update() {
   foreach (Character character in scene_Characters) {
      character.Update();
   }
}
// ********************



//Script #2:

namespace RPG {

public class Character {
   public int ID;
   public string Name;
   public GameObject gameObject;
   public Stats stats = new Stats();

  public void Initialize() {
     //Make stuff start
  }

  public void Die() {
     this.gameObject.Destroy(); //among other things you'll need to do here
  }

  public void Update() {
    if (stats.CurHP <= 0)
      Die();
  }
}

public class Stats {
   public int CurHP;
   public int MaxHP;

  public void ChangeHP(int difference) {
     CurHP += difference;
     if (CurHP > MaxHP)
        CurHP = MaxHP;
     if (CurHP <= 0)
        CurHP = 0;
  }
}

}

P.S - Untested and kind of got carried away.

This sets you up for saving and loading of data though, because now all you’ll have to do is save and load your scene_Characters list and when you load a game re-initialize your scene character manager script.

SendMessage (and all it’s family) is the worst imho.
nice for beginner explanation purposes only.

Look into interfaces, I found it the best for damage systems.