Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by phyzxengr · Jul 05, 2020 at 06:15 AM · unity 5classesmethodsstatic-variables

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

227 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How do I use JS to access C# methods/classes? 2 Answers

How can you list all classes inside a namespace (such as UnityEngine) and every message method that class has? 1 Answer

How to select a variable from another GameObject by accessing the class in the Inspector. 2 Answers

Class Reference to Other Class does not allow me to use its Methods 1 Answer

Change calling object with extension method 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges