Values not updating in Unity. - UPDATED
UPDATE: I had some progress, but I am still having issues. Updated.
OnClick functions do work also with the Debug.Log. The code below is the Stats script which also updates my HealthUI.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
[Serializable]
public class Stats
{
public static Stats st;
[SerializeField]
private BarScript bar;
public float maxVal;
public float currentVal;
public int playerDamage = 20;
public float CurrentVal
{
get
{
return currentVal;
}
set
{
this.currentVal = Mathf.Clamp(value, 0, maxVal);
bar.Value = currentVal;
}
}
public float MaxVal
{
get
{
return maxVal;
}
set
{
this.maxVal = value;
bar.MaxValue = value;
}
}
public void Initialize()
{
this.MaxVal = maxVal;
this.CurrentVal = currentVal;
}
}
I did progress a bit. I placed this code:
public void HpUp () { stats.maxVal += 20; stats.currentVal += 20; }
public void DmgUp() { stats.playerDamage += 10; }
into my PlayerManager and it connects with the Stats script I shared and my health does update. I removed the UpgradeMenuUI. Now the problem is that my damage does not update. Well actually it updates but the value doesn't reach where I need it to. I added a
public int playerDamage;
in my Stats script. In my Damage script, which calculates the damage for my Player and the enemy, I changed
aiManager.Enemy1Damage(playerDamage); to
aiManager.Enemy1Damage(stats.playerDamage);
and I call my playerDamage from Stats into my Damage script with these: public Stats stats; and
public void Awake()
{
stats = new Stats();
}
So the problem is that if I change the playerDamage in my Stats script from this: public int
playerDamage;
to
public int playerDamage = 20;
The value does work and my Player damage is indeed 20 when I hit the AI. However in the inspector my damage(the public int from Stats script) is 0 and if I press my upgrade damage button it goes up by 10 (that's what I set in my PlayerManager script:
public void DmgUp()
{
stats.playerDamage += 10;
}
), but that doesn't affect in any way the value 20. So it is just useless, like pumping air. I don't know how to make the public int playerDamage to update from the OnClick. If make it just playerDamage; without a number nothing updates and the damages is 0.
Answer by jeango · Nov 13, 2017 at 09:39 PM
replace
stats.maxVal = (int)(stats.maxVal += hpUp);
by
stats.maxVal+= hpUp;
You're not using the += operator correctly
edit: Sorry, I tested this and your code should work... however you're doing some insanely weird things.
Why do you have a property (MaxVal) with a getter and setter for a public variable (maxVal)? The whole point of making a property is to encapsulate a private variable. And if you have a property, why not use it by doing stats.MaxVal += hpUp ?
I guess this project is just a test project, could you upload it so I could make some testing and see what it looks like? I think you must have done something wrong elsewhere
Edit2:
Can you explain why you made a MaxVal property that modifies bar.MaxValue in the setter method? You don't seem to use that property anywhere, and I think this is part of the puzzle.
Just so that we're clear, there's a HUGE difference between doing
stats.maxVal += 10;
// with a small m. this only updates your maxVal public variable
and
stats.MaxVal += 10;
// with capital M. this calls the setter method of your MaxVal property, which updates maxVal AND bar.MaxValue
Also, why do you have a static reference to a Stats instance in your Stats class? Are you trying to make a singleton?
You made your Stats class serializable, so I take it from this that you wrote a custom inspector script for it (otherwise it just wouldn't be visible in the inspector). Care to share your custom inspector code? This might be where the problem is.
I have a really hard time to understand the structure of your project. Can you share it in its entierty?
if all you're trying to do is to update a value with a press of a button, here's a very simple sample code that will do what you're apparently trying to do:
the stats class
public class Stats {
public int value;
}
And the class that will modify its value
public class StatsUpdater : MonoBehaviour {
private Stats stat = new Stats ();
public int increment = 10;
// subscribe this method to your OnClick event
public void AddIncrement() {
stat.value += increment;
}
}
The suggested code by you was the original code I had. Well basically I was following this tutorial: https://www.youtube.com/watch?v=b-C$$anonymous$$$$anonymous$$TAP2lQ&list=PLPV2$$anonymous$$yIb3jR42oVBU6$$anonymous$$2DIL6Y22Ry9J1c∈dex=35 , I am playing with a 2D fighter game and I have a Player and an Enemy with some big fighting scripts for combos etc. Then I added a damage system and a Health UI. Stats screen so from the UI tutorial but I don't remember where exactly. Since I am pretty new to unity its quite a mess probably. Everything worked fine, but I wanted to have an update screen where I can boost the damage and health of the player. I tried a few things around whats shown in the tutorial and although no errors and correct function calls, the values do not update in Unity.
Where would it be nice to upload the whole project. I am a real beginner so I want to see what you have to say.
You can attach a file to a post (just click on the little trombone)