Author Topic: Clicking a button that increments a displayed value.  (Read 5315 times)

bigsiegee

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 7
    • View Profile
Clicking a button that increments a displayed value.
« on: October 10, 2014, 01:21:37 AM »
Greetings everyone.

This should be a fairly simple question, I've tried my darndest to get this working but must be missing something simple.

What I am trying to set up is a Coloured Button called "Buy 10 Gold". When it is clicked it adds it to a label/prefab in the top right corner. Every time "Buy 10 Gold" is clicked it should increase the displayed Gold. At the moment it displays "0" in the _gold.text box in the top right.

I have created a Panel called MarketPlace. Inside that Panel I have a Button called "Buy 10 Gold) and a prefab/label called FloatingText). I have added an event trigger to "Buy 10 Gold" and pointed it to Panel: MarketPlace which has the c# MarketPlace.cs attached to it. The Button: Buy 10 Gold increments the var int Gold every time it is clicked but does not update it in the top right.

Basically all I'm looking for is - click a button, increment a gold label/prefab in the top right corner.

Thanks heaps for your help.

Kind regards

A complete noob :(

Below is the code I've been struggling with:

  1. public class MarketPlace : MonoBehaviour {
  2.         public int Gold = 0;
  3.         private UILabel _gold;
  4.  
  5.         void Start () {
  6.                 _gold = GetComponent<UILabel> ();
  7.                 _gold.text = ("" + Gold);
  8.         }
  9.        
  10.         public virtual void Gold10 () {
  11.                 Gold = Gold +=10;
  12.                 }
  13. }
  14.  

ionutztamas

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 1
  • Posts: 6
    • View Profile
Re: Clicking a button that increments a displayed value.
« Reply #1 on: October 10, 2014, 01:35:50 AM »
Try calling

  1. _gold.text = ("" + Gold);

again after you increment the Gold value;

  1. Gold += 10;
is enough, you don't need to do Gold = Gold += 10;

bigsiegee

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 7
    • View Profile
Re: Clicking a button that increments a displayed value.
« Reply #2 on: October 10, 2014, 01:50:26 AM »
Thanks.

I tried sticking the "_gold.text = ("" + Gold);" under "Gold +=10" all it achieves is that gold only increments once to 10. I can click it many times and it wont increase any further.

The two big problems I have right now are:
1) I don't actually have anything displaying gold in the top right hand corner. Ages ago it worked briefly but it was either the numbers would increment and not appear or I could get it to display a set value only.
2) When it does increment the gold variable it wont update in the top right. Right now my code isn't even displaying a top right value :(

Any more ideas perchance?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Clicking a button that increments a displayed value.
« Reply #3 on: October 10, 2014, 02:53:59 AM »
This is a scripting question, not an NGUI question. You should be asking this on Unity support forum, not here, but...

Attach this script to your button.
  1. using UnityEngine;
  2.  
  3. public class IncreaseMyGold : MonoBehaviour
  4. {
  5.     public UILabel label; // <-- set this in inspector
  6.     public int myGold = 0;
  7.  
  8.     void Start () { label.text = myGold.ToString(); }
  9.  
  10.     void OnClick ()
  11.     {
  12.         myGold += 10;
  13.         label.text = myGold.ToString();
  14.     }
  15. }

bigsiegee

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 7
    • View Profile
Re: Clicking a button that increments a displayed value.
« Reply #4 on: October 10, 2014, 04:46:54 AM »
Thanks heaps! Worked an absolute charm.

I guess I've been watching too many NGUI tutorials all day on how to get an NGUI button to change a sprite's colour, transform its size, shape, rotation and all that mumbo jumbo. Was thinking changing text would be up the same tree.

Thanks heaps once again :)

Adam.

P.S

I know I probably shouldn't be asking this here, but just a query on your code. I made another button, 2 buttons in total. B1: 10g, B2: 100g. If I click B1 twice I get 20g. If I click B2 twice I get 200g. They display their values independently at the moment. I would like it so if I click B1 and B2 twice I would get a combined value of 220.

Thanks again for your help with this.

  1. public class IncreaseMyGold : MonoBehaviour
  2. {
  3.         public UILabel label; // <-- set this in inspector
  4.         public int myGold = 0;
  5.         public int goldAmount;
  6.        
  7.         void Start () { label.text = myGold.ToString(); }
  8.        
  9.         void OnClick ()
  10.         {
  11.                 if (goldAmount == 1) {
  12.                                 myGold += 10;
  13.                                 label.text = myGold.ToString ();
  14.                         } else if (goldAmount == 2) {
  15.                                 myGold += 100;
  16.                                 label.text = myGold.ToString ();
  17.                         }
  18.                 }
  19. }
« Last Edit: October 10, 2014, 10:45:22 PM by bigsiegee »