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 /
This question was closed Sep 01, 2016 at 02:42 AM by jodyruben for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by jodyruben · Sep 01, 2016 at 02:04 AM · c#uibuttonif-statementsvoid

make increment and decreasement button with if statement didn't work?

!Hi, i have some mini project which it makes some increment and decreasement in it. so i make a button to make a soldier increase and decrease by pressing plus and min button. however , it didn't work , i try to debug but nothing happens. so maybe a void cannot using if statement ? please make it clear for me . thanks !

this the script i use: using UnityEngine; using System.Collections; using UnityEngine.UI; using UnityEngine.SceneManagement;

 public class CastleGo : MonoBehaviour {
 
     public Text curInfantQty;
     public Text curArchQty;
     public Text curCavQty;
 
 
     public int curInfant;
     public int curArch;
     public int curCav;
 
     // Use this for initialization
     void Start () {
         curInfant = CastleQty.infantryQty;
 
         curInfantQty.text = "" + curInfant;
         curArchQty.text = "" + CastleQty.archerQty;
         curCavQty.text = "" + CastleQty.cavalryQty;
 
 
     }
         
     public void plusInfant()
     {
         if (curInfant > CastleQty.infantryQty) {
             curInfant++;
             Debug.Log (curInfant);
         } else {
             return;
         }
     }
 
     public void minInfant()
     {
         if (curInfant <= 0) {
             curInfant--;
             Debug.Log (curInfant);
         } else {
             return;
         }
     }
 }
 

and the button i wish to make simply like this:

alt text

i already doing my stuff with the button setting like attach it to the script. still didn't figure it out.

unity.png (278.5 kB)
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

  • Sort: 
avatar image
1
Best Answer

Answer by Bonfire-Boy · Sep 01, 2016 at 02:21 AM

Looks to me like the logic is the wrong way round. You only decrease the value if it's already negative, and only increase it if it's already larger than the value you initialised it to. So, assuming that CastleQty.infantryQty is non-negative on initialisation, both plusInfant() and minInfant() are going to return without doing anything.

Also note that if you want the displayed value to change, you need to set curInfantQty.text again.

Comment
Add comment · Show 4 · 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
avatar image jodyruben · Sep 01, 2016 at 02:29 AM 0
Share

oh yeah , figure that one. but still didn't work. i think the void can't work with if statement? if i put the if statement , the debug.log didnt show up on debug console. but if i commented it out, the debug log works just fine. so im thinking the void can't work with if statement ? please clear this one out xD thanks

avatar image Bonfire-Boy jodyruben · Sep 01, 2016 at 02:34 AM 0
Share

I'm sorry but it's not clear to me what you mean by "the void can't work with if statement". There is no "void" connected with your if statements, in fact the only "voids" in the code are return types of functions and they certainly won't impact on the logic within the functions.

But if you've changed the code and it still doesn't work, you should share the new code. We're not $$anonymous$$d-readers :)

avatar image jodyruben · Sep 01, 2016 at 02:35 AM 0
Share

never$$anonymous$$d i forgot to update it with text . sorry for bothering xD and how i can make your answer is the answer? cant do it through comment. thanks!

avatar image Bonfire-Boy jodyruben · Sep 01, 2016 at 02:38 AM 0
Share

I've converted it to an answer you should be able to accept it now.

Follow this Question

Answers Answers and Comments

221 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

Related Questions

I made an resume button, but I don't know how to code it to close my pause menu, any tips? 2 Answers

I need to move a sprite when an UI button is clicked 1 Answer

How to keep your button selected after clicking away ??? 1 Answer

UI Button color 1 Answer

Hidden Canvas, doesn't disable interaction with buttons. 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