Search Unity

How to make in ui text when typing in game inside that it iwll not overwrite ?

Discussion in 'UGUI & TextMesh Pro' started by SharonL75, Sep 22, 2020.

  1. SharonL75

    SharonL75

    Joined:
    Aug 13, 2020
    Posts:
    91
    If I'm typing something in the Inspector inside the ui text it looks like that :

    upload_2020-9-22_20-41-14.png

    But when I'm clicking the keypad numbers in the game it self the numbers are overwrite each other and it's showing each time only one number :

    For example I clicked on 1 and then on 2 but 2 replaced 1 instead adding the 2 near the 1 :

    upload_2020-9-22_20-43-17.png

    On each key in the keypad I added this script :

    Code (csharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using System.Text.RegularExpressions;
    5. using UnityEngine;
    6.  
    7. public class SecurityKeypadKeys : MonoBehaviour
    8. {
    9.     public TextMesh text;
    10.  
    11.     private void Start()
    12.     {
    13.        
    14.     }
    15.  
    16.     private void OnMouseDown()
    17.     {
    18.         string[] digits = Regex.Split(transform.name, @"\D+");
    19.         foreach (string value in digits)
    20.         {
    21.             int number;
    22.             if (int.TryParse(value, out number))
    23.             {
    24.                 text.text = number.ToString();
    25.             }
    26.         }
    27.     }
    28. }
    29.  
     
  2. JuliaP_Unity

    JuliaP_Unity

    Unity Technologies

    Joined:
    Mar 26, 2020
    Posts:
    700
    You have to add to the current text, not completely replace it:

    text.text += number.ToString();


    Also remember to clear the first time you're replacing the placeholder with actual input, this would always just add it to the end :)
     
    SharonL75 and Avalin like this.