Player's Heath is not decreasing

As I mentioned in the title in lecture no. 106 Laser Defender when the player gets hit by the lasers of enemy the health of the player is not decreased I have checked there is nothing wrong in the laser but in the player.
Please help me
I am sharing my code here.


I have cropped the code.

1 Like

I don’t see any issue with your code with the exception that you didn’t refactor it as Rick did. (Or so the Git says)

This might be a little hard to track. So let me ask you a few questions:

  • Does your console shows any errors?
  • Have you checked the damage of the Damage Dealer? It might be set to 0.
  • Are the lasers actually colliding with the ship? A good way to check that would be by doing this:
  private void OnTriggerEnter2D(Collider2D other)
    {
        Debug.Log(other.transform.name); //Add this line
        DamageDealer damageDealer = other.gameObject.GetComponent<DamageDealer>();
...

That will print the name of the object in the console if it is colliding, if the console isn’t printing anything then there’s your issue. There be might some problems with your colliders’ settings.

1 Like

Thanks for replying Yee i have checked that the damage of the laser is not set to 0. I will try writing the code block you have shared with me i will reply if the console prints anything.

And yes whenever i write new code block there is a message coming saying …will always have its null value 0
I click clear and they vanish away. I just ignore them . Is there any problem with the messages coming. And thanks for replying Yee

1 Like

Those warning messages in the console are nothing to worry about.

If this doesn’t help, create a Unity Package, upload it and post a download link (or send me the link in a PM if you are more comfortable with that) here for me to check your project out (That or copy-paste all your codes related to the player taking damage and screenshots of the settings of your player, enemies, and missiles, I suggest the package because it’s a lot easier than doing all that).

To create a Unity package you just have to go to Assets / Export Package… Then a window will open containing all your files, keep everything selected and then just click on Export…

Hi i just checked the player in my Hierarchy and the collider wasn’t there actually i added the collider in the prefab but forgot to apply it.

So i have added the collider and now it works fine.
Sorry for wasting your time.
I am really stupid.
Sorry and thanks for helping me.
The community is just awesome.
And thanks for your help

Thanks Yee but i have already found my mistake
And corrected it.
I have posted a meesage on telling my silliness.
And thanks for your help

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.