Need help Damage / Death

Hello.

I have lots of Code here but basically i have followed a tutorial and created an energy/health/shield bar. They reduce based on their health % value (so 100 of 1000 hp will display 10% HP).

My damage code is not working. I need it to deal damage and react with the above features, and destroy the player when at 0 hp.

}
void damaged()
{
health–;
if(health <-0) Destroy.this.gameObject;
}
void OnCollisionEnter(Collision collision)
{
Enemy enemy = Collision.Collider.GetComponent();
if(enemy!=null)
{
damaged();

if (Input.GetKeyDown(KeyCode.W))
{
health.CurrentValue -= 10;
}
}
}

Any feedback would be great.
Thanks!

Use code tags

You forgot a lot of things, like does it give you errors
(I’m sure this does: Destroy.this.gameObject, it’s Destroy(gameObject), parentheses and you don’t need the this)
< - 0 ? you mean < 0?

Ok making a little progress, i think i have the damage stuff down, but im getting issues.

  1. im not dealing damage
  2. I get an error after the first shot

Please Advise

here is all the code i am using

For Firing

using System.Collections;
using System.Collections.Generic;
using UnityEngine;


public class firemechanic : MonoBehaviour {

public Rigidbody projectile;
public float speed = 20;

// Use this for initialization
void Start () {

}

// Update is called once per frame
void Update () {

        if (Input.GetButtonDown ("Fire1")) {
            Rigidbody instantiatedProjectile = Instantiate (projectile,
                                                  transform.position,
                                                  transform.rotation)
            as Rigidbody;

            instantiatedProjectile.velocity = transform.TransformDirection (new Vector3 (0, 0, speed));

        }
    }
}

for the projectile

using System.Collections;
using System.Collections.Generic;
using UnityEngine;



public class fire : MonoBehaviour

{
    public float damage = 100f;
    public float speed;
    public Rigidbody rigidbody;

    void Start ()
    {
        rigidbody = GetComponent<Rigidbody>();
        rigidbody.velocity = transform.forward * speed;
    }

    void Update(){
    }

    void OnCollisionEnter (Collision collision)
    {
        if (collision.gameObject.tag == "Enemy")
        {
            collision.gameObject.SendMessage("TakeDamage", damage, SendMessageOptions.DontRequireReceiver);

        }
    }
}

For Life

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class lifesystem : MonoBehaviour {
    public float hitPoints = 5000f;
    public float currentHitPoints;
    public GameObject destroyfx;

    // Use this for initialization
    void Start () {
        currentHitPoints = hitPoints;
    }

    public void TakeDamage(float amt)
    {
        currentHitPoints -= amt;
        if(currentHitPoints <= 0)
            {
                currentHitPoints = 0;
                Die();
            }
    }

    public void Die()
    {
        Debug.Log ("Enemy Has died");

    }

    // Update is called once per frame
    void Update () {
       
    }
}
[B]

[/B]

But what type of error?

1 Like

If you change to SendMessageOptions.RequireReceiver do you get any error messages? Are you able to find “TakeDamage”?

Because the code inside TakeDamage looks fine

Im thinking it has something to do with the reference to the object.
When i fire i get an error referencing to another HP Script i have. I tried removing it but it does not seem to resolve the issue (Strangely).

Does the collision / rigged body need to be active on the prefab, or the mesh itself?
I think the issue is here. I noticed that some times the objects hit the other mesh target, absorbing the shot, yet not dealing damage.

like an entire week, and no resolution to this. Forums really suck for help.

Agreed.

1 Like

No they don’t, we asked you what the error was, did you answer? NO, we’re not compilers, we won’t look at your 150 lines of code one by one just to see what kind of problem you have if you don’t even take the time to tell us the error you’re getting

If someone posts 20 questions and none of them gets answered, then he can say this, but this was your first post, why?

4 Likes
void damaged()
{
    health--;
    if(health <-0)
        Destroy.this.gameObject;
}
void OnCollisionEnter(Collision collision)
{
    Enemy enemy = Collision.Collider.GetComponent<Enemy>();
    if(enemy!=null)
    {
        damaged();
    }
    if (Input.GetKeyDown(KeyCode.W))
    {
        health.CurrentValue -= 10;
    }
}

Even if I take your original provided script, it doesn’t make much sense. Some of the parts simply don’t work.

Wrong use of operator and wrong usage of Destroy.

//Original
if(health <-0)
    Destroy.this.gameObject;
//Probably your intention
if(health <= 0)
    Destroy(this.gameObject)

Using type instead of parameter

//Original
void OnCollisionEnter(Collision collision)
{
    Enemy enemy = Collision.Collider.GetComponent<Enemy>();
//probably your intention
void OnCollisionEnter(Collision col)
{
    Enemy enemy = col.gameObject.GetComponent<Enemy>();

Placing code where it shouldnt be

//Input should always be used under Update, and not anywhere else.
    if (Input.GetKeyDown(KeyCode.W))
    {
        health.CurrentValue -= 10;
    }
1 Like

I’m not sure exactly how your code is written. But i would guess that your enemy has the lifesystem attached to it. Try to get the lifesystem component from the enemy when the collision occures and call the TakeDamage method from there, instead of sending a message.

collision.gameobject.getcomponent().TakeDamage(damage);

1 Like

Actually, that is not my code. It was a tutorial on youtube. It works in the video, im assuming it may be outdated.

like iv said, if been through a lot of ways to do this, and none of it is working

So if you use this code in your fire class, what happens?

void OnCollisionEnter (Collision collision)
    {
        if (collision.gameObject.tag == "Enemy")
        {
            collision.gameObject.GetComponent<lifesystem>().TakeDamage(damage);

        }
    }

Nothing. It seems to not register the damage.

The Projectile and the ship both have rigid and collides applied.

Is the collision registered? Try to type to the log to see if the code is ever executed. It could be that you haven’t set the tag for you enemy maybe?

void OnCollisionEnter (Collision collision)
  {
      Debug.Log("Collision");
      if (collision.gameObject.tag == "Enemy")
      {
            Debug.Log("Enemy Collision");
            collision.gameObject.GetComponent<lifesystem>().TakeDamage(damage);
      }
  }