- Home /
Why can't I change/access stats inside C# code?
I am trying to create a 2D turn-based RPG game in Unity, I ran into an obstacle though. I have a panel where I can upgrade my stats with plus and minus buttons. The wanted change is shown by counters next to the buttons. Besides this upgrade panel I also have a stats panel which shows my current stats. I am trying to use a 'Submit' button to validate the changes. However when this button is pressed and the Submit() function is called the wanted value does not change. I tried to print everything to console to find where the process has troubles. The ChangeCounters do increase (and decrease) and they are also being shown. I assume that even the Submit() is being called, however it doesn't actually work. When I used submit with a "hard-coded" number like 10, it changed the stat properly though. What have I done wrong? How could I write this is in a better way? Could you guys please help me with this?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Stats : MonoBehaviour
{
protected int Weapon = 1;
[SerializeField]
protected Text WeaponText;
//there are multiple of these in my real code for different stats
protected int WeaponChangeCounter = 0;
[SerializeField]
protected Text WeaponCCText;
//there are multiple of these in my real code for different stats
void Update()
{
ShowUpgrade();
UpdateStats();
}
public void UpdateStats()
{
WeaponText.text = Weapon.ToString();
}
public void ShowUpgrade()
{
WeaponCCText.text = WeaponChangeCounter.ToString();
}
public void increase(int module)
{
switch (module)
{
case 1:
WeaponChangeCounter++;
break;
//I have other cases(stats) here in my real code
default:
break;
}
}
public void decrease(int module)
{
switch (module)
{
case 1:
WeaponChangeCounter--;
break;
//I have other cases(stats) here in my real code
default:
break;
}
}
public void Submit()
{
Weapon += WeaponChangeCounter;
//Weapon += 10; <=== works properly
}
}
I don't see any issues with your code. What have you done already to debug your issue? Don't say you haven't yet placed some Debug.Log statements to check the values of Weapon / WeaponChangeCounter at various positions of your code...
Are you sure that you do not have any other code that might reset the WeaponChangeCounter before you apply it? Again what have you done to debug your issue? You also know that you can switch the inspector into debug mode? In debug mode you can actually see the values of private / protected variables as well. However any custom editor you might have will be disabled in this case.
I did place some Debug.Log statements to check the values, I even placed some to check which functions were called. I created a whole new project with only this code and just the 2 texts (weapon, changecounter) and the 3 buttons (plus, $$anonymous$$us, submit). There is literally nothing else in there, still not working. I did not know about the debug mode of the inspector, I will look into that now. Any other advice?
Update: I started using the debug mode and immediately noticed that the ChangeCounter variable is only changing in the PlusButton game object's script. Probably that's why the Submit button cannot use the reference (?) When I added the script to the Canvas (to have a mutual game object for both of the buttons) I could see the value change. However It is not being shown on the display.
Answer by TheForeigner01 · Dec 15, 2019 at 12:28 AM
!!!SOLVED!!! The Problem was not in the code, the buttons' onClick() had different game objects assigned to them which caused a difference in the values of the variables. This way the ChangeCounters were increased but they were unable to be accessed. ( I still don't know why I could not actually see the values displayed in my test project though... luckily they work properly in the main one)
Your answer
Follow this Question
Related Questions
Can't interact with world space ui button when cursor mode is locked any solutions? 1 Answer
Graphics Raycaster - Click on overlapped buttons 1 Answer
Unity5 UI - How to trigger button click event while preventing menu item deselect event? 1 Answer
Displaced UI RectTransform collider in some resolutions issue. 1 Answer
How can I have a UI ScrollRect still scroll if pressing a button inside of it? 1 Answer