- Home /
Does any one know why this happening - Unity keeps messing with my Math
Hello
i'm working on my character stats. I'm trying to deal damage to their energy when they move, using the left, right, up and down keys.
The problem is that Unity keeps multiplying by 100, instead of simply taking out energy every time the character moves. (-1 energy per frame for now, i will fix the deduction speed later) (or if you have any ideas those are welcomed.)
here is the code for the character stats script.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CharacterStats : MonoBehaviour
{
//a list of the various stats
public List<BaseStats> HappinessStats = new List<BaseStats>();
public List<BaseStats> EnergyStats = new List<BaseStats>();
public List<BaseStats> SmartsStats = new List<BaseStats>();
public List<BaseStats> LooksStats = new List<BaseStats>();
private int EnergyValue;
public int energyMinus;
public string walking;
void Start()
{
// set the values of each stat list
//HAPPINESS
HappinessStats.Add(new BaseStats(100, "Happiness: ", "Your Happiness level"));
//HappinessStats[0].AddStatBonus(new StatBonus(5));
HappinessStats[0].AddStatBonus(new StatBonus(-50));
Debug.Log("Happiness" + HappinessStats[0].GetCalculatedStatValue());
//ENERGY
EnergyStats.Add(new BaseStats((100), "Energy: ", "Your Energy level"));
Debug.Log("ENERGY" + EnergyStats[0].GetCalculatedStatValue());
//EnergyValue = EnergyStats[0].GetCalculatedStatValue();
//SMARTS
SmartsStats.Add(new BaseStats(100, "Smarts: ", "Your Intelligence level"));
//LOOKS
LooksStats.Add(new BaseStats(100, "Looks: ", "Your Looks level"));
}
void Update()
{
//have energy reduce as the character walks.
IsWalking();
}
//checks if the character is walking and returns a boolean
public void IsWalking()
{
if ((Input.GetAxis("Horizontal") != 0 && Input.GetAxis("Vertical") != 0) || (Input.GetAxis("Horizontal") != 0 || Input.GetAxis("Vertical") != 0))
{
energyMinus += -1;
EnergyStats[0].RemoveStatBonus(new StatBonus(energyMinus));
Debug.Log(EnergyStats[0].GetCalculatedStatValue());
walking = "WE ARE WALKING";
}
else
{
energyMinus += 1;
EnergyStats[0].RemoveStatBonus(new StatBonus(energyMinus));
Debug.Log(EnergyStats[0].GetCalculatedStatValue());
walking = "WE ARE NOT WALKING";
}
}
}
here is the code for Base stats that creates a stat bonus list and adds or removes bonuses (walking damage)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BaseStats
{
public List<StatBonus> BaseAdditives { get; set; }
public int BaseValue { get; set; }
public string StatName { get; set; }
public string StatDescription{ get; set; }
public int FinalValue { get; set; }
public BaseStats(int baseValue, string statName, string statDescription)
{
//each stat, has an empty list of stat bonuses
this.BaseAdditives = new List<StatBonus>();
this.BaseValue = baseValue;
this.StatName = statName;
this.StatDescription = statDescription;
}
//ADD STAT BONUS FROM LIST
public void AddStatBonus(StatBonus statBonus)
{
this.BaseAdditives.Add(statBonus);
}
//REMOVE STAT BONUS FROM LIST
public void RemoveStatBonus(StatBonus statBonus)
{
this.BaseAdditives.Remove(statBonus);
}
//CALCULATE THE FINAL VALUE OF STAT
public int GetCalculatedStatValue()
{
this.BaseAdditives.ForEach(x => this.FinalValue += x.BonusValue);
FinalValue += BaseValue;
/*
for (int i = 0; i < BaseAdditives.Count; i++)
{
this.BaseAdditives.ForEach(x => this.FinalValue += x.BonusValue);
FinalValue += BaseValue;
}
*/
return FinalValue;
}
}
Answer by Siccity · May 12, 2020 at 10:46 AM
I don't know what's happening in your script, but I can assure you, Unity is not multiplying your numbers without you telling it to.
Try picking apart your script bit by bit, and see if you can isolate the issue.
I already did that. I’ve simplified the code and the issue persists. I’m not sure what to do.
The code is generally assigning stats to a player and when they player walks it is supposed to remove 1. Unfortunately that isn’t the case. As it just keeps adding 100.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
My character stats script doesn't identify my variables 3 Answers
NPCs following an uneven floor... 0 Answers
How can I make character assets walk a certain distance? 2 Answers