Increase and Decrease text.text values

I need 2 methods for increasing and decreasing text object value .
There are 2 ui button , each one has one method .
when right button is clicked and its method is called i need , text object text which shows a number , increases 1 unit …
and left button method decrease it 1 unit as it also is clicked.
base value or number is 1 so numbers below 1 are not accepted , and the highest number is 5 and numbers higher than 5 also are not accepted .
i am working around about 1 week on this so far and could not find a logic for that . following code is what i have got up to now , but it does not function correctly :

public int prices;
    public Text text;
    public int max = 30000;
    public int min = 10000;

    public void Start()
    {
    //    prices = min;
    }
    public void increaseprice()
    {
        // Is prices less than max?
        if (prices < max)
        {
            // If yes, then increase prices.
            prices = prices + 10000;
            // Is prices now greater than max?
            if (prices > max)
            {
                // If so, set prices too max.
                prices = max;
            }
            // Now update the text.
            setvalue();
        }
    }

    public void decreaseprice()
    {
        // Is prices greater than min?
        if (prices > max)
        {
            // If yes, then increase prices.
            prices = prices - 10000;
            // Is prices now lower than min?
            if (prices < min)
            {
                // If so, set prices too min.
                prices = min;
            }
            // Now update the text.
            setvalue();
        }
    }

       
    void setvalue()
    {
        text.text = prices.ToString();
    }

Not sure if understood all the features, but here’s my try:

2721984--193277--decrease-increase.gif

Source:

using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class DecreaseIncrease : MonoBehaviour
{
    public Text text;
    public Button btnDecrease; // added reference to buttons so can disable them
    public Button btnIncrease;

    public int max = 50000;
    public int min = 10000;

    int currentAmount = 0;
    int increasePerClick = 10000;

    // from button click event, call AdjustValue(true) if want to increase or AdjustValue(false) to decrease
    public void AdjustValue(bool increase)
    {
        // clamp current value between min-max
        currentAmount = Mathf.Clamp(currentAmount + (increase ? increasePerClick : -increasePerClick), min, max);
        text.text = currentAmount.ToString();

        // disable buttons if cannot increase/decrease
        btnDecrease.interactable = currentAmount > min;
        btnIncrease.interactable = currentAmount < max;
    }
}
1 Like

It does not work correctly . By first click it simply goes one step forwards , then disable left button and adds 10000 , then goes to 30000 by forth click , clicking on left button shows number 10000 and that’s all . i set everything correctly in editor . ))

Any idea?

Can make the start amount 10000 to avoid that, but not really sure what else it should do…

mgear its not still working, but thanks alot . if others have suggestions , pleased you .

The script provided by @mgear works as I’d expect from your description @yariangames .
You’ve either made a mistake when setting up the buttons or you haven’t described your problem properly.

One thing I’d also add is the evaluation of the current value in Awake or Start, so that either the increase button or the decrease button may already be disabled if you start out with currentAmount == min or currentAmount == max respectively.
However, that doesn’t change alot and is just a small addtition.

No , its not working for me . decrease method does not work at all . what i should type in inspector public min and max field for decrease ui button?at the moment increase ui button public min max field have 10000 and 30000 respectively. maybe here is the problem , coz with debug.log it show method is correct but calculations does not run correctly.

Sorry I cannot reproduce what you’re observing.
Have you taken the exact same code from above?
The method is meant to work for both buttons, the increase button additionally needs the checkbox to be checked when you add the method to the listeners.

you know, i need a class for creating in game shop store items . there are some items in an horizontal list which are draggable . below these items there is price text and select item button . do you have any idea how to implement it , thanks for your attentions.

There are lots of different ways to do that, but is it directly related to the original topic?
Either way, you might want to practice a little more before you start to think about things such as ingame shops etc.

ok . thanks.

I know this post is old, but I need your help. Once I copied down all that code, where would I assign the script to? Both buttons?

assign to one object only (doesnt need to be in the buttons), then in the button click event for each button do that:
// from button click event, call AdjustValue(true) if want to increase or AdjustValue(false) to decrease

        public void AdjustValue(bool increase)
    public class Button AdjustValue(true);
    public class Button AdjustValue(false);
        {

What? so like this? gotta explain the adjustvalue thing better I’m very inexperienced with c#.

in the inspector you can assign what happens when button is clicked,
so on this image, you could drag the gameobject with the script on that object field,
then select the DecreaseIncrease.AdjustValue in the dropdown,
then for 1 button make the parameter [×] true, for the other false (below the dropdown)
3049914--228809--upload_2017-4-28_10-28-11.png

It worked! Thanks so much for the help and sorry for bothering.