C# how to make other script apply the damage (75750)

i didn’t get any errors but it won’t apply the damage.

this is the script:

using UnityEngine;
using System.Collections;

public class ZombieScript : MonoBehaviour {
	
	public int health = 100;
	private int damage;
	private float time;
	private float hrdelay = 0.5f;
	public int healthRegen = 1;
	
	// Use this for initialization
	void Start () {
	
	}
	
	
	public void Damage (int damageParam){
		damage = damageParam;
	}
	
	// Update is called once per frame
	void Update () {
	health = health - damage;
	damage = 0;
	if (Time.time > time)
		{
		time = Time.time + hrdelay;
		health = health + healthRegen;
		}
	}
}

2 Answers

2

Your method looks pretty good. I would just apply the damage in the Damage function. This would keep you from needing to store the damage as a variable of the zombie, which makes sense.

 public void Damage (int damageParam){
        health = health - damageParam;
}

Can you show the code where you are calling the damage function from another class? Everything in here seems fine.

Shouldn't it be otherScript.Damage(damage)? Also, this code is only going to work for one zombie. You're going to need to implement it in a way that applies this damage to the exact zombie that you hit. Try this CollidedZombie = hit.collider.transform.parent.gameObject; answer from( http://answers.unity3d.com/questions/157538/find-parent-of-a-collider.html)

> when i shoot when i play the game and > shoot a zombie i get an error code. You said in the original question that you didn't get an error.

definitely post the error message. That's what will help track it down.

NullReferenceException UnityEngine.GameObject.GetComponent[ZombieScript] () (at C:/BuildAgent/work/7535de4ca26c26ac/Runtime/ExportGenerated/Editor/UnityEngineGameObject.cs:27) TestGunScript.Update () (at Assets/TestGunScript.cs:55) thats the error code

OK. post what is in line 55 so I can see what is null. Try adding this piece of code to your project and tell me what it reveals. GameObject CollidedZombie = GameObject.Find ("Zombie"); if(collidedZombie == null){Debug.Log("Didn't get a zombie) } else { ZombieScript otherScript = CollidedZombie.GetComponent(); if(otherScript != null) { otherScript.DoDamage(damage);} else{ Debug.Log(The zombie didn't have the script attached to it) }; }

Look at what you wrote on line 20. CollidedZombie is the first thing called “Zombie” in your scene. That’s not going to work if there are more than 1 Zombie. It needs to be assigned to “hit.collider.gameObject”.

Name variables with lowercase and functions with uppercase. No exceptions.
collidedZombie” instead of “CollidedZombie” and “ApplyDamage” instead of “dammage”.

That will work now, assuming ZombieScript works. But you didn’t post your “dammage” function from ZombieScript so there’s no way of us knowing whether you botched that one too…

yeah he posted the "dammage" function in the original post. He misspelled it though in the Update function. I think you misinterpret the function. He was trying to use it as a setter, but I completely agree that something like "ApplyDamage" or "DoDamage" would work better. As for CollidedZombie, I agree it "should" be lower case 'c', but that isn't going to solve his problem.

i did what you said

Did it help solve the problem?

no it didn't