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:
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;
}
}
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 . ))
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.
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
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)