Hello I’m creating a similar game to bit life, but i’m having issues finding any good resources that helps.
I’m trying to make stats that can be applied to the player and the NPCs when they are generated. There should be 4 - Joy, energy, brains and appearance.
I started off with energy, because when the character moves around I want the energy to reduce. and when they eat or stop moving, their health increases. So far below is the code i’ve got.
This class should calculate the amount of Energy left, based on the maxEnergy and currentEnergy values sent from the player/NPC class.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnergyCal : MonoBehaviour
{
//VARIABLES
public List<customSort> energySort = new List<customSort>();
// Update is called once per frame
void Update()
{
IsWalking();
}
public void IsWalking()
{
energySort.Add(new customSort("Default", 1, true));
energySort.Add(new customSort("Walking", 2, false));
//check if character is standing or walking
if (Input.GetAxis("Horizontal") != 0 || Input.GetAxis("Vertical") != 0)
{
energySort[0].isRemoving = true;
if (Input.GetKey(KeyCode.LeftShift))
{
energySort[1].isRemoving = true;
}
else
{
energySort[1].isRemoving = false;
}
}
if (Input.GetAxis("Horizontal") == null || Input.GetAxis("Vertical") == null)
{
Debug.Log("heloo");
}
}
//CALCULATES THE REMOVED ENERGRY
public int removeEnergy(int maxEnergy, int currentEnergy)
{
foreach (customSort e in energySort)
{
if (e.isRemoving)
{
currentEnergy += (int)(e.value / 100);
}
}
maxEnergy -= (int)(currentEnergy * Time.deltaTime);
if (maxEnergy <= 0)
{
//die
Debug.Log("YOU DIED");
}
return maxEnergy;
}
}
This is the stats Bar class.
/*---- This file does:
* Holds the status for all characters in the game
*/
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class StatsBar : MonoBehaviour
{
public StatsVariables charEnergy;
public EnergyCal Energy;
void Start()
{
charEnergy.Max = 100;
charEnergy.Current = charEnergy.Max;
}
// Update is called once per frame
void Update()
{
//taking damage from walking and prints out the value
Energy.IsWalking();
charEnergy.Current = Energy.removeEnergy(charEnergy.Max, charEnergy.Current);
Debug.Log(charEnergy.Current);
}
}
I keep getting this error when I add the stats class to my character, when he moves around the value doesn’t reduce aswell. Any ideas?
NullReferenceException: Object reference not set to an instance of an object
StatsBar.Update () (at Assets/Scripts/Character Scripts/StatsBar.cs:28)