- Home /
Enemy AI won't take Damage
I have a simple combat script set up, but everytime the prefab hits the enemy the enemies health will not go down. I could use any help I'm at the verge of giving up on this I can't figure it out.
public var path1 : Transform;
public var path2 : Transform;
public var enemySpeed : int = 6;
public var currentHealth : int = 1;
public var maxHealth : int = 1;
private var target : Transform;
private var controller : CharacterController;
function Start () : void
{
SetTarget(path1);
controller = GetComponent(CharacterController);
currentHealth = maxHealth;
gameObject.renderer.material.color = Color.red;
}
function SetTarget(newTarget : Transform) : void
{
target = newTarget;
}
function Update () : void
{
var lookAtPosition : Vector3 = new Vector3 (target.position.x, this.transform.position.y, target.position.z);
transform.LookAt(lookAtPosition);
controller.SimpleMove(transform.forward*enemySpeed);
}
function OnTriggerEnter (node : Collider) : void
{
if (node.transform == target)
{
if (target == path1)
SetTarget(path2);
else if (target == path2)
SetTarget(path1);
}
}
function ModifyHealth (change : int) : void
{
{
currentHealth += change;
if (currentHealth > maxHealth)
currentHealth = maxHealth;
else if (currentHealth <= 0)
Destroy(this.gameObject);
}
}
this is the bullet script
var shot : GameObject;
function Start()
{
}
function OnCollisionEnter(info : Collision) : void {
if(info.collider.tag == "Enemy"){
info.collider.SendMessageUpwards("ModifyHealth", -1);
}
}
function Update () {
if(Input.GetButtonDown("Fire1"))
{
var porjectile = Instantiate(shot, transform.position, Quaternion.identity);
porjectile.rigidbody.AddForce(transform.forward * 1500);
}
}
Have you tried taking out the void function types? I don't think they are required.
$$anonymous$$aybe, not relevant :P
In any case, you will need to add in a whole bunch of
Debug.Log("Stuff" + variables);
to see whats going on.
I don't understand could you please show me an example?
function OnCollisionEnter(info : Collision) {
Debug.Log("Collision Detected");
if(info.collider.tag == "Enemy"){
info.collider.Send$$anonymous$$essageUpwards("$$anonymous$$odifyHealth", -1);
Debug.Log("Enemy Hit");
}
}
Use Debug.Log as stated above to check whether or not the prefab actually does anything + make sure your colliders are set to trigger
Answer by ozone · Dec 08, 2013 at 11:35 PM
Perhaps try using info.gameObject.SendMessageUpwards() instead of info.collider.SendMessageUpwards(). That should ensure that the message reaches all scripts attached to the enemy game object. You will also need to use if(info.gameObject.tag == "Enemy") instead of what you have now, because tag is not a member of collider.
Your answer
![](https://koobas.hobune.stream/wayback/20220613123730im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
How can an enemy deplete players health? 0 Answers
ZOMBIE AI SCRIPT 1 Answer
AI script not using gravity 1 Answer
Player Health drops fast, how to pause after damage 0 Answers