Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

One ui text with various numbers

Discussion in 'Scripting' started by yariangames, Jul 11, 2016.

  1. yariangames

    yariangames

    Joined:
    Jun 8, 2016
    Posts:
    53
    how is it possible to change ui text content (in this case numbers) by clicking on an arrow ui button respectively from 1 till 5? each step it adds 1 value untill it gets for example to 5 ?
    thanks.
     
  2. Korno

    Korno

    Joined:
    Oct 26, 2014
    Posts:
    518
    Have an integer as a variable of the monobehaviour. Have two methods, one that increments this number and one the decrements the number. Hook up the events to each button - You cand do this in the editor OnClick property for the button. Have a third method witch sets the text to this number (ToString()). The increment method and decrements method should call this method after they modify the number.

    You might consider a slider as a better UI design for something like this though. One slide action as apposed to multiple clicks of multiple buttons.
     
  3. yariangames

    yariangames

    Joined:
    Jun 8, 2016
    Posts:
    53
    Korno thanks for your help, but i cannot use slider though while i have those 2 arrows, u know. also onclick from arrow ui button already is used by another script. can you please write that code so so generally and i will fulfill the rest as you said by methods and other things. only i need the base form . thanks.
     
  4. Korno

    Korno

    Joined:
    Oct 26, 2014
    Posts:
    518
    You can have multiple Handlers for the OnClick method of a button by the way.

    I cant write any code for you as I don't have my dev box with me now. Try yourself first and I will take a look if you struggle.
     
  5. yariangames

    yariangames

    Joined:
    Jun 8, 2016
    Posts:
    53
    ok man i will start it out right now. will tell you by problems as if it is so. anyway,thanks na?!
     
  6. yariangames

    yariangames

    Joined:
    Jun 8, 2016
    Posts:
    53
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class changeprices : MonoBehaviour
    5. {
    6.  
    7.     public int prices;
    8.  
    9.  
    10.     public void increaseprice()
    11.     {
    12.        
    13.     }
    14.  
    15.     public void decreaseprice()
    16.     {
    17.  
    18.     }
    19.  
    20.     void setvalue()
    21.     {
    22.        
    23.     }
    24. }
    this is up to this point, is it correct, how to increment some integer value?
     
  7. Korno

    Korno

    Joined:
    Oct 26, 2014
    Posts:
    518
    something like this:

    Code (CSharp):
    1.   public class changeprices : MonoBehaviour
    2.     {
    3.  
    4.         public int prices;
    5.  
    6.         public UnityEngine.UI.Text text;
    7.  
    8.         public int max = 5;
    9.         public int min = 0;
    10.  
    11.         public void Start()
    12.         {
    13.             if (text == null) throw new ArgumentException("text not set ");
    14.             if (max < min) throw new ArgumentException("max < min...");
    15.             if (max == 0) throw new ArgumentException("max = 0");
    16.             // start in the middle;
    17.             prices = max / 2;
    18.         }
    19.  
    20.         public void increaseprice()
    21.         {
    22.             prices = prices == max ? max : prices++;
    23.             setvalue();
    24.         }
    25.  
    26.         public void decreaseprice()
    27.         {
    28.             prices = prices == min ? min: prices--;
    29.             setvalue();
    30.         }
    31.  
    32.         void setvalue()
    33.         {
    34.             text.text = prices.ToString();
    35.         }
    36.     }
    make sure you hook up increaseprice and decreasprice in the editor.
     
    Last edited: Jul 11, 2016
  8. yariangames

    yariangames

    Joined:
    Jun 8, 2016
    Posts:
    53
    The type or namespace name `ArgumentException' could not be found. Are you missing a using directive or an assembly reference?
    whats the problem?