- Home /
Need help with Player stats for Choose your own story game
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)
Answer by lino1000 · May 11, 2020 at 03:01 PM
Its because your variable Energy never gets a value. You try to call the IsWalking method of it at line 28 but you never give the variable Energy itself a value. You need to do something like write in the awake Energy = FindObjectOfType(); or drag what you want your energy variable to be inside the unity editor since it is public.
Also its a bad idea to write variables with a capital letter. I would rename it to energy (with small e at the beginning)
Hello thanks. I ended up watching a different tutorial and changed how I structured the stats code.
Could you actually assist me with a new issue that popped up, which is Unity messing around with my math. Here is a link to the question I asked:
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Moving a character depending on where it is facing. 1 Answer
Add Special Events for Skills 1 Answer