- Home /
Having problems with damage script
Hi i'm trying to make a damage script where when an arrow hits the target that bodypart will take damage. However i can't seem to get this to work and i don't know why.
Hit script
private void OnTriggerEnter(Collider other)
{
Embed();
if (other.CompareTag("Animal"))
{
transform.parent = other.transform;
Debug.Log(other.name);
Health hitHealth = other.GetComponent<Health>();
if (hitHealth != null)
{
hitHealth.TakeDamage(arrowDamage);
}
}
Debug.Log(other.name);
}
TakeDamage Script
public float partHealth;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
public void TakeDamage (float amount)
{
partHealth -= amount;
Debug.Log("I " + transform.name + " took " + amount + " damage, and have " + partHealth + " hp left");
if (partHealth <= 0)
{
Debug.Log(transform.name + " part died");
}
}
}
There doesn't seem to be anything wrong with the collisions since when i hit an object it returns the name but i can't affect the script in any way :/
Answer by Casiell · Oct 10, 2018 at 07:17 PM
Ok, but where exactly does it fail? Code looks ok. Are you sure there is a Health component on other gameobject? Is arrowDamage greater than zero? Does the other object have "Animal" tag?
Also if a collider is on the body part which is a children of an object with Health component, you will not succeed. In this case you will have to use GetComponentInParent
The body parts themselves have individual health points. There is a Health script on the other game object i even tried adding a rigidbody ( someone said somewhere that it would work) but to no avail. The component does have an "Animal" tag The arrowDamage is currently at 100 which whould instantly kill the body part but nothing happends still.
Thank you so much for your time trying to fix it! but it still doesn't seem to work :(
Ok, so you have to debug the script. Put a breakpoint at the beginning, shoot an arrow and see where it stops.
Also it's enough to have rigidbody on one object only, in this case your arrow probably has one so it should be enough.
Your answer
Follow this Question
Related Questions
Damage on prefab collision? 0 Answers
Take health from enemy 3 Answers
Why can't my character jump 2 Answers
OnCollisionEnter Sine Wave to Character 0 Answers
Damaging Enemy strangely not working. 0 Answers