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

If I’m typing something in the Inspector inside the ui text it looks like that :

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 :

On each key in the keypad I added this script :

using System.Collections;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using UnityEngine;

public class SecurityKeypadKeys : MonoBehaviour
{
    public TextMesh text;

    private void Start()
    {
       
    }

    private void OnMouseDown()
    {
        string[] digits = Regex.Split(transform.name, @"\D+");
        foreach (string value in digits)
        {
            int number;
            if (int.TryParse(value, out number))
            {
                text.text = number.ToString();
            }
        }
    }
}

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 :slight_smile:

2 Likes