Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Problem with scripting HP system

Discussion in 'Scripting' started by MertEmrem_, Aug 21, 2016.

  1. MertEmrem_

    MertEmrem_

    Joined:
    Jul 21, 2016
    Posts:
    2
    So I'm going through Unity's space shooter tutorial, and wanted to give the spaceship more than one chance to get hit, but for some reason my code doesn't work. I could use some help, thanks.


    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class PlayerDeath : MonoBehaviour {
    5.     public GameObject playerExplosion;
    6.     public GameObject Explosion;
    7.     public GameObject Asteroid;
    8.     public float Hp = 5f;
    9.  
    10.     void Start () {
    11.  
    12.     }
    13.    
    14.     void OnCollisionEnter(Collider other)
    15.     {
    16.  
    17.         if (other.gameObject == Asteroid)
    18.         {
    19.             Destroy(other.gameObject);
    20.             Hp = Hp - 1;
    21.             Instantiate(Explosion, other.transform.position, other.transform.rotation);
    22.         }                      
    23.  
    24.         if (Hp == 0)
    25.         {
    26.             Destroy(gameObject);
    27.             Instantiate(playerExplosion, transform.position, transform.rotation);
    28.         }      
    29.     }
    30. }
    31.  
     
  2. lloydsummers

    lloydsummers

    Joined:
    May 17, 2013
    Posts:
    344
    Check that you also increase HP in the inspector
     
  3. GNGification

    GNGification

    Joined:
    Oct 24, 2013
    Posts:
    59
    What exactly doesn't work? Also to prevent bugs later, you should change

    Code (csharp):
    1. if(Hp == 0)
    to
    Code (csharp):
    1. if(Hp <= 0)
    It may even fix your current problem, if health is already 0, because the upper if statement is ran first, making it -1.
     
  4. Cynikal

    Cynikal

    Joined:
    Oct 29, 2012
    Posts:
    122
    I think your issue is you're using: if(other.gameObject== Asteroid)

    if I remember correctly, that'll never fire, because it's looking for the very specific prefab you put. (If it's in the scene, it's not the same).

    I'd do something like: if(gameObject.tag == "Asteroid") or .name == "Asteroid".

    Also, as GNGification mentioned, you should change if(Hp == 0) to if(Hp <= 0) (If HP is less than, or equal to 0).

    Also, (doesnt make a difference), you could do:

    Hp -= 1; or Hp--; Instead of Hp = Hp - 1;
     
  5. MertEmrem_

    MertEmrem_

    Joined:
    Jul 21, 2016
    Posts:
    2
    Thanks, Cynikal! That fixed it!
     
    lloydsummers likes this.