0
votes

UPDATE: The code from the answer didn't work but I had some progress, but I am still having issues. Updated.

This is my UpgradeUI script:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

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.

1
The issue remains. - TheNewbie

1 Answers

1
votes

You have the Stats as a Singleton but are accessing a new instance of it on your button. Try changing:

public void HpUpgrade()
{
    stats.maxVal = (int)(stats.Instance.maxVal += hpUp);

}
public void DmgUpgrade()
{
    dmg.playerDamage = (int)(stats.Instance.maxVal += dmgUp);
}

I can't quite tell if you've confirmed that your OnClick event is triggering for the buttons. If they aren't, make sure that you have the EventSystem game object in your scene.