Search Unity

Trouble accessing a UI gameobject and modifying when entering a trigger.

Discussion in 'Scripting' started by aaeriam, Jun 13, 2016.

  1. aaeriam

    aaeriam

    Joined:
    Sep 17, 2014
    Posts:
    10
    I'm trying to set up a health bat that goes down whenever the player takes damage. Just for testing purposes, I created an empty game object and used that as a trigger to decrease the health bar when the player walks into it.

    Here's the code attached to the health bar UI


    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.UI;
    4.  
    5.  
    6. public class HealthBarController : MonoBehaviour {
    7.  
    8.     public Image healthBar;
    9.     private float startHealth = 1.0f;
    10.     private float currentHealth;
    11.  
    12.     // Use this for initialization
    13.     void Start ()
    14.     {
    15.         currentHealth = startHealth;
    16.  
    17.         GetHealth();
    18.     }
    19.  
    20.     void GetHealth ()
    21.     {
    22.         healthBar.fillAmount = currentHealth;
    23.     }
    24. }

    and here's the code on the trigger that I want to use to decrease the player's health bar when they enter it.


    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.UI;
    4.  
    5. public class TestDamageScript : MonoBehaviour
    6. {
    7.     HealthBarController HPcontroller;
    8.  
    9.     void Start ()
    10.     {
    11.         HPcontroller = GetComponent<HealthBarController>();
    12.     }
    13.  
    14.     void OnTriggerEnter2D (Collider2D entity)
    15.     {
    16.         if (entity.tag == "Player")
    17.         {
    18.             HPcontroller.healthBar.fillAmount -= 0.5f;
    19.         }
    20.     }
    21.  
    22. }

    whenever I run the scene, everything works as it should until I enter the trigger where it throws back:

    NullReferenceException: Object reference not set to an instance of an object
    TestDamageScript.OnTriggerEnter2D (UnityEngine.Collider2D entity) (at Assets/Scripts/TestDamageScript.cs:18)

    I know the error is probably my logic behind the HPcontroller.healthBar.fillAmount. I'm just not sure what to do to start fixing it.
     
  2. LTK

    LTK

    Joined:
    Jul 16, 2015
    Posts:
    22
    I think fillAmount have value from 0 to 1 :D.
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4. using UnityEngine.UI;
    5.  
    6. public class TestDamageScript : MonoBehaviour
    7. {
    8.     HealthBarController HPcontroller;
    9.     void Start ()
    10.    {
    11.         HPcontroller = GetComponent<HealthBarController>();
    12.    }
    13.     void OnTriggerEnter2D (Collider2D entity)
    14.     {
    15.         if (entity.tag == "Player")
    16.         {
    17.           var value = HPcontroller.healthBar.fillAmount - 0.5f;
    18.           if(value < 0)
    19.                 value = 0;
    20.           HPcontroller.healthBar.fillAmount = value;
    21.         }
    22.     }
    23. }
    24.