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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Healthbar : MonoBehaviour
{
[SerializeField] Slider slider;
public void SetMaxEnergy(float energy)
{
slider.maxValue = energy;
slider.value = energy;
}
public void SetEnergy(float energy)
{
slider.value = energy;
}
}
Energybar inspector:

The Player(although i put all the code the problem is in the beggining lines 20 and 35):
[SerializeField] GameObject projectile_Prefab;
[SerializeField] GameObject firePoint;
[SerializeField] float playerSpeed = 10f;
[SerializeField] float jumpSpeed = 10f;
[SerializeField] bool isGrounded;
[SerializeField] bool inAir;
[SerializeField] int flightTrigger;
[SerializeField] float energy;
[SerializeField] float maxEnergy = 100f;
Rigidbody2D rb;
Healthbar healthbar;
// Start is called before the first frame update
void Start()
{
healthbar = GetComponent<Healthbar>();
rb = GetComponent<Rigidbody2D>();
healthbar.SetMaxEnergy(maxEnergy);
}
// Update is called once per frame
void FixedUpdate()
{
Move();
}
private void Update()
{
Fire();
Jump();
healthbar.SetEnergy(energy);
}
private void Move()
{
//Normal 2d movement of Ironman
float moveHorizontally = Input.GetAxis("Horizontal");
Vector2 movement = new Vector2(moveHorizontally * playerSpeed, rb.velocity.y);
rb.velocity = movement;
FlipSprite();
//How the Flight() triggers
if(inAir == true && flightTrigger == 2)
{
Flight();
}
else if (flightTrigger > 2)
{
flightTrigger = 1;
//Changes back to the normal 2d movement rigidbody
}
}
private void Flight()
{
//Changes to Kinematic rigidbdy so it doesnt use gravity
//Horizontal and Vertical input references
float moveHorizontally = Input.GetAxis("Horizontal");
float moveVertically = Input.GetAxis("Vertical");
//The vector that uses them multiplied by the playerSpeed
Vector2 movement = new Vector2(moveHorizontally, moveVertically) * playerSpeed;
rb.velocity = movement;
energy--;
if (energy <= 0)
{
flightTrigger = 0;
}
FlipSprite();
}
private void Jump()
{
if (isGrounded == true && Input.GetKeyDown(KeyCode.Space))
{
rb.velocity = Vector2.up * jumpSpeed;
}
if (Input.GetKeyDown(KeyCode.Space))
{
flightTrigger++;
}
}
private void Fire()
{
if (Input.GetButtonDown("Fire1"))
{
energy--;
GameObject projectile = Instantiate(projectile_Prefab, firePoint.transform.position, transform.rotation) as GameObject;
}
}
private void FlipSprite()
{
if (Input.GetAxis("Horizontal") > 0)
{
transform.localRotation = Quaternion.Euler(0, 0, 0);
}
else if (Input.GetAxis("Horizontal") < 0)
{
transform.localRotation = Quaternion.Euler(0, 180, 0);
}
}
private void OnCollisionEnter2D(Collision2D collisionenter)
{
if(collisionenter.gameObject.CompareTag("Ground"))
{
flightTrigger = 0;
isGrounded = true;
inAir = false;
}
}
private void OnCollisionExit2D(Collision2D collisionexit)
{
if (collisionexit.gameObject.CompareTag("Ground"))
{
isGrounded = false;
inAir = true;
}
}
private void Regen(float energytoadd)
{
energy += energytoadd % 2 ;
ReachMaxEnergy();
}
private void AddEnergy(float energytoadd)
{
energy += energytoadd;
ReachMaxEnergy();
}
private void SubtractEnergy(float energytoadd)
{
energy -= energytoadd ;
ReachMaxEnergy();
}
private void ReachMaxEnergy()
{
if (energy > maxEnergy)
{
energy = maxEnergy;
}
}
private void OnCollisionStay2D(Collision2D collision)
{
if (collision.gameObject.CompareTag("Ground"))
{
Regen(0.5f);
}
else if (collision.gameObject.CompareTag("Pickup"))
{
Regen(2f);
}
}
}
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