- Home /
Can I increment a static variable from a static method?
After trying to be responsible and doing the research on my own I am now fed up. I will keep this short and simple. I want to be able to use the class.method() style of access instead of instantiation or inspector drag and drop. I have:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
public class AccountManager : MonoBehaviour
{
public static float m_TradeAccount = 1000000000000.00f;//Billion
//public static TextMeshPro m_textMesh;
public static TextMeshPro m_AccountBalanceTMP;
public static GameObject m_AccountBalanceGO;
//https://forum.unity.com/threads/static-class-or-singleton.106694/
//https://docs.unity3d.com/ScriptReference/RuntimeInitializeOnLoadMethodAttribute-ctor.html
[RuntimeInitializeOnLoadMethod]
static void OnRuntimeMethodLoad()
{
Debug.Log("Before scene loaded");
}
void Awake()
{
Debug.Log("AccountManager.Awake()");
//Debug.Log("*" + m_AccountBalanceGO.name + "*" + m_AccountBalanceTMP.name + "*");
}
// Start is called before the first frame update
void Start()
{
//m_TradeAccount = 1000000000000.00f;//Billion
m_AccountBalanceGO = GameObject.Find("AccountBalance");
if (m_AccountBalanceGO == null) Debug.Log("AccountBalanceGO not found");
m_AccountBalanceTMP = m_AccountBalanceGO.GetComponent<TextMeshPro>();
if (m_AccountBalanceTMP == null) Debug.Log("AccountBalanceTMP not found");
Debug.Log("*"+ m_AccountBalanceGO.name +"*" + m_AccountBalanceTMP.name+"*");
Account_display();
}
public static void Account_display()
{
Debug.Log("Display AccountManager.m_TradeAccount: " + m_TradeAccount.ToString("$000,000,000,000,000,000.0000000000000000000"));
m_AccountBalanceTMP.text = m_TradeAccount.ToString("$000,000,000,000,000,000");//Trillions
}
public static void AccountIncrease()
{
m_TradeAccount = m_TradeAccount + 100.0f;
Debug.Log("Increase m_TradeAccount: " + m_TradeAccount.ToString("$000,000,000,000,000,000.0000000000000000000"));
Account_display();
}
public static void AccountDecrease()
{
m_TradeAccount = m_TradeAccount - 100.0f;
Debug.Log("Decrease m_TradeAccount: " + m_TradeAccount.ToString("$000,000,000,000,000,000.0000000000000000000"));
Account_display();
}
}
The secondary script calls AccountManager.AccountIncrease(): I know this works. But the dangest thing is the variable doesn't increase or decrease; Now I need serious mental help and coding support. I am cross-eyed at all the static, polymorphism and delegate preacher's C explanations. There has got to be a simple post for the simplest of answers about this. I want this post to be it. There are a lot of posts about the myriad ways to do this but my style represents the simplest way. So a developer could simply in another script write a class.method(); call. Thank you all for any responses.
Answer by phyzxengr · Jul 05, 2020 at 05:44 PM
public static double m_TradeAccount = 1000000000000.00f;//Billion
Changed from float to double. I remain chastised in my humility. And now I must return to my clown car. Thank you all for your patience and should any of you choose to whip me severely in my ignorance I kneel at the Flagellation pillar for the desired session. Thank you for your trouble.