Damage to player

Hi,

I am going through the Ruby Adventure tutorial on unity learn.

I wrote the code like they instructed me to for the Damage stuff.

When I hit the Damage sprite, my health goes down. Then when I pick up health, my health goes up. But then when I go back to touch the damage sprite, my health stays the same and no damage is done.

Any idea?

The damage script:

 private void OnTriggerStay2D(Collider2D other)
    {
        RubyController controller = other.GetComponent<RubyController>();
        Debug.Log("Z Controller stuff" + other);


        if (controller != null)
        {

            Debug.Log("THis is the new shit");
            controller.ChangeHealth(-1);
        }
    }

Can’t see any problem in this code. It must be somewhere else. Haven’t you disabled collider in another script accidentaly?

what?
i have another script, the same code attached to the health which has this additional function
Destroy(gameobject)
but its not attached to the damage sprite

I mean, if there’s another script on the player wich disables the collider or rigidbody so collisions stop happening. By the way, you have two Debug.Log lines in your code. Do you see the output of those lines in the console when go back and touch sprite again and again?

i dont know

i added some code to make the character invincible for a few seconds after it hits the damage sprite.

and now it works. now when i get back on the damage sprite i get the debug.log info.

I hate coding. I removed the code and it stops working.

i dont know, just forget it. thx anyway.

Lol. I think you invincibility script is the reason. It makes character invincible and do not revert those changes properly back when it’s time runs out.

1 Like

i found it.
it works fine.

i have to unpress collapse in the console of unity to get the messages, so the code was working fine all along,

facepalm