Search Unity

Simple Energy Bar not working ("Object reference not set to an instance of an object")

Discussion in '2D' started by pedromrnosorio97, May 6, 2021.

  1. pedromrnosorio97

    pedromrnosorio97

    Joined:
    Mar 11, 2020
    Posts:
    1
    Hi everyone, first time posting.
    Im having trouble with my energybar working accordingly with my player's current and max energy.
    Here is my code and screen shots:


    Energybar
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class Healthbar : MonoBehaviour
    7. {
    8.     [SerializeField] Slider slider;
    9.  
    10.  
    11.  
    12.     public void SetMaxEnergy(float energy)
    13.     {
    14.         slider.maxValue = energy;
    15.  
    16.         slider.value = energy;
    17.     }
    18.     public void SetEnergy(float energy)
    19.     {
    20.         slider.value = energy;
    21.     }
    22. }
    Energybar inspector:
    Captura de ecrã 2021-05-06 103230.png


    The Player(although i put all the code the problem is in the beggining lines 20 and 35):
    Code (CSharp):
    1. [SerializeField] GameObject projectile_Prefab;
    2.     [SerializeField] GameObject firePoint;
    3.     [SerializeField] float playerSpeed = 10f;
    4.     [SerializeField] float jumpSpeed = 10f;
    5.     [SerializeField] bool isGrounded;
    6.     [SerializeField] bool inAir;
    7.     [SerializeField] int flightTrigger;
    8.     [SerializeField] float energy;
    9.     [SerializeField] float maxEnergy = 100f;
    10.  
    11.  
    12.     Rigidbody2D rb;
    13.     Healthbar healthbar;
    14.  
    15.     // Start is called before the first frame update
    16.     void Start()
    17.     {
    18.         healthbar = GetComponent<Healthbar>();
    19.         rb = GetComponent<Rigidbody2D>();
    20.         healthbar.SetMaxEnergy(maxEnergy);
    21.  
    22.     }
    23.  
    24.     // Update is called once per frame
    25.     void FixedUpdate()
    26.     {
    27.         Move();
    28.      
    29.      
    30.     }
    31.     private void Update()
    32.     {
    33.         Fire();
    34.         Jump();
    35.         healthbar.SetEnergy(energy);
    36.  
    37.     }
    38.  
    39.     private void Move()
    40.     {
    41.         //Normal 2d movement of Ironman
    42.         float moveHorizontally = Input.GetAxis("Horizontal");
    43.         Vector2 movement = new Vector2(moveHorizontally * playerSpeed, rb.velocity.y);
    44.         rb.velocity = movement;
    45.         FlipSprite();
    46.         //How the Flight() triggers
    47.         if(inAir == true && flightTrigger == 2)
    48.         {
    49.             Flight();
    50.         }
    51.         else if (flightTrigger > 2)
    52.         {
    53.             flightTrigger = 1;
    54.             //Changes back to the normal 2d movement rigidbody
    55.          
    56.         }
    57.      
    58.     }
    59.  
    60.     private void Flight()
    61.     {
    62.         //Changes to Kinematic rigidbdy so it doesnt use gravity
    63.      
    64.         //Horizontal and Vertical input references
    65.         float moveHorizontally = Input.GetAxis("Horizontal");
    66.         float moveVertically = Input.GetAxis("Vertical");
    67.         //The vector that uses them multiplied by the playerSpeed
    68.         Vector2 movement = new Vector2(moveHorizontally, moveVertically) * playerSpeed;
    69.         rb.velocity = movement;
    70.      
    71.         energy--;
    72.      
    73.         if (energy <= 0)
    74.         {
    75.             flightTrigger = 0;
    76.         }
    77.         FlipSprite();
    78.  
    79.      
    80.     }
    81.  
    82.     private void Jump()
    83.     {
    84.         if (isGrounded == true && Input.GetKeyDown(KeyCode.Space))
    85.         {
    86.             rb.velocity = Vector2.up * jumpSpeed;
    87.          
    88.         }
    89.  
    90.         if (Input.GetKeyDown(KeyCode.Space))
    91.         {
    92.             flightTrigger++;
    93.         }
    94.  
    95.     }
    96.  
    97.     private void Fire()
    98.     {
    99.         if (Input.GetButtonDown("Fire1"))
    100.         {
    101.             energy--;
    102.             GameObject projectile = Instantiate(projectile_Prefab, firePoint.transform.position, transform.rotation) as GameObject;
    103.  
    104.         }
    105.     }
    106.  
    107.     private void FlipSprite()
    108.     {
    109.         if (Input.GetAxis("Horizontal") > 0)
    110.         {
    111.             transform.localRotation = Quaternion.Euler(0, 0, 0);
    112.          
    113.         }
    114.         else if (Input.GetAxis("Horizontal") < 0)
    115.         {
    116.             transform.localRotation = Quaternion.Euler(0, 180, 0);
    117.          
    118.         }
    119.     }
    120.  
    121.  
    122.  
    123.     private void OnCollisionEnter2D(Collision2D collisionenter)
    124.     {
    125.         if(collisionenter.gameObject.CompareTag("Ground"))
    126.         {
    127.             flightTrigger = 0;
    128.             isGrounded = true;
    129.             inAir = false;
    130.        
    131.          
    132.         }
    133.  
    134.     }
    135.  
    136.     private void OnCollisionExit2D(Collision2D collisionexit)
    137.     {
    138.         if (collisionexit.gameObject.CompareTag("Ground"))
    139.         {
    140.             isGrounded = false;
    141.             inAir = true;
    142.         }
    143.     }
    144.  
    145.     private void Regen(float energytoadd)
    146.     {
    147.         energy += energytoadd % 2 ;
    148.         ReachMaxEnergy();
    149.     }
    150.     private void AddEnergy(float energytoadd)
    151.     {
    152.         energy += energytoadd;
    153.         ReachMaxEnergy();
    154.     }
    155.     private void SubtractEnergy(float energytoadd)
    156.     {
    157.         energy -= energytoadd ;
    158.         ReachMaxEnergy();
    159.     }
    160.  
    161.     private void ReachMaxEnergy()
    162.     {
    163.         if (energy > maxEnergy)
    164.         {
    165.             energy = maxEnergy;
    166.         }
    167.     }
    168.  
    169.  
    170.  
    171.     private void OnCollisionStay2D(Collision2D collision)
    172.     {
    173.      
    174.         if (collision.gameObject.CompareTag("Ground"))
    175.         {
    176.             Regen(0.5f);
    177.         }
    178.         else if (collision.gameObject.CompareTag("Pickup"))
    179.         {
    180.           Regen(2f);
    181.         }
    182.      
    183.     }
    184. }
    185.  

    I hope i gave all the information correctly

    Like i metioned before the 2 erros appering are this "NullReferenceException: Object reference not set to an instance of an object". If someone can point out what im doing wrong or if im missing
    something i would be grateful for the help. Thank you for your time.

    Edit: I noticed that i named things incorrectly, when im referring to healthbar on the script its meant to be energybar but i mistankenly wrote it like that because i was following the video of Brackeys on youtube. Basically where it says health bar its supposed to be energy bar
     
    Last edited: May 6, 2021
  2. Kazen

    Kazen

    Joined:
    Feb 17, 2015
    Posts:
    68
    You can only use GetComponent for healthbar if the script is on the same object as the player. If it is not, you have to put 'public' in front of your healthbar variable and manually drag your energy bar object to your players script. Eg 'public Healthbar healthbar' and drag your object in the editor