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

Why isn't my health decreasing?

Discussion in 'Scripting' started by switchluts, Jan 9, 2020.

  1. switchluts

    switchluts

    Joined:
    Mar 18, 2019
    Posts:
    45
    Target script
    Code (CSharp):
    1. {
    2.     public float targetTime = 6.0f;
    3.     public float targetCont;
    4.     public health health;
    5.         // Start is called before the first frame update
    6.     void Start()
    7.     {
    8.         targetCont = Time.time + targetTime;
    9.     }
    10.  
    11.     // Update is called once per frame
    12.     void Update()
    13.     {
    14.       if (Time.time > targetCont)
    15.         {
    16.             Destroy(gameObject);
    17.             health.healthX--;
    18.         }
    19.  
    20.     }
    21.  
    22. }
    health script
    Code (CSharp):
    1. {
    2.     public int healthX;
    3.     public int numOfHearts;
    4.     public Image[] hearts;
    5.     public Sprite fullHeart;
    6.     public Sprite emptyHeart;
    7.     // Start is called before the first frame update
    8.     void Start()
    9.     {
    10.  
    11.     }
    12.  
    13.     // Update is called once per frame
    14.     void Update()
    15.     {
    16.         if (healthX > numOfHearts)
    17.         {
    18.             healthX = numOfHearts;
    19.         }
    20.         for (int i = 0; i < hearts.Length; i++)
    21.         {
    22.             if (i < healthX)
    23.             {
    24.                 hearts[i].sprite = fullHeart;
    25.             }
    26.             else
    27.             {
    28.                 hearts[i].sprite = emptyHeart;
    29.             }
    30.  
    31.             if (i < numOfHearts)
    32.             {
    33.                 hearts[i].enabled = true;
    34.  
    35.             }
    36.             else { hearts[i].enabled = false; }
    37.         }
    38.  
    39.     }
    40. }
     
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Add debugging and/or actually describe the problem. For all we know you have multiple instances of the same script in the scene and you're checking the wrong one.
     
  3. switchluts

    switchluts

    Joined:
    Mar 18, 2019
    Posts:
    45
    My target is an instantiated object and every 6 seconds it get destroyed when that happens I want to decrease heath by one
     
  4. AcidArrow

    AcidArrow

    Joined:
    May 20, 2010
    Posts:
    11,064
    Not sure if that's the only issue, but try switching places to these two lines:
    1. Destroy(gameObject);
    2. health.healthX--;
     
  5. switchluts

    switchluts

    Joined:
    Mar 18, 2019
    Posts:
    45
    here's the error btw
    Object reference not set to an instance of an object
    target.Update ()
     
  6. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Shouldn't matter, as Destroy just queues up the destruction of the GameObject for it to occur after the current frame completes.

    Mentioning you had an error in the console probably should have been in your original post. Where are you setting "health" to the correct reference before you try to use it?

    Also, don't name variables identical to the name of their class.
     
  7. AcidArrow

    AcidArrow

    Joined:
    May 20, 2010
    Posts:
    11,064
    You're right.
     
  8. kinguhdahood5

    kinguhdahood5

    Joined:
    Dec 18, 2018
    Posts:
    84
    I believe you need to assign the component of the health script. in the Start function of the target script, add this:
    Code (csharp):
    1.  
    2. health = GetComponent<health>();
    3.  
     
  9. switchluts

    switchluts

    Joined:
    Mar 18, 2019
    Posts:
    45
    didn't work sadly
     
  10. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    That won't work if the health component is attached to a different GameObject. Set the reference to the correct component on the correct GameObject, wherever that is, in Start or do it via the inspector if these GameObjects are part of the scene or part of the same prefab.

    https://docs.unity3d.com/Manual/ControllingGameObjectsComponents.html