Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 TheForeigner01 · Dec 14, 2019 at 11:25 PM · uibuttonrpgupgradestats

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
     }
 }

Comment
Add comment · Show 2
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
avatar image Bunny83 · Dec 14, 2019 at 11:38 PM 1
Share

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.

avatar image TheForeigner01 Bunny83 · Dec 15, 2019 at 12:05 AM 0
Share

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.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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)

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

216 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

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


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