- Home /
Display text on gameobject via user inputs
Hello! I have a telephone model and i am trying the numbers to be changed as the user is dialing a phone number. So far i have managed to display every phone button pressed on the telephone screen.But when i press the same button again (e.g. 1), it is sot redisplayed, as it should.
here is the code.
 using UnityEngine;
  using System.Collections;
  
  public class TextController : MonoBehaviour {
  
     public draw3 buttons;
     private string displayNum;
     private string num;
     public string n1, n2, n3, n4, n5, n6, n7, n8, n9, n0, st, sh;
 
     private TextMesh testMesh;
 
     void Update () {
         if (Input.GetMouseButton (0)) {
             if (buttons.button.gameObject.name == "big_drawers_Plane.037_1") {
                 n1 = "1";
                 num = n1;
             }
             if (buttons.button.gameObject.name == "big_drawers_Plane.037_2") {
                 n2 = "2";
             }
             
             if (buttons.button.gameObject.name == "big_drawers_Plane.037_#") {
                 sh = "#";
             }
         }
         else
             n1 = "";
 
         displayNum = num+n2+sh;
         testMesh = GetComponent<TextMesh> ();
         testMesh.text = displayNum;
     }
 }
               Comment
              
 
               
               
               Best Answer 
              
 
              Answer by allenallenallen · Dec 22, 2016 at 01:41 AM
Rather than using such a weird and complicated way of displaying the phone number, you should just append the newly pressed number to the displayNum.
 void Update () {
          if (Input.GetMouseButton (0)) {
              if (buttons.button.gameObject.name == "big_drawers_Plane.037_1") {
                  displayNum += "1";
              }
              if (buttons.button.gameObject.name == "big_drawers_Plane.037_2") {
                  displayNum += "2";
              }
              
              if (buttons.button.gameObject.name == "big_drawers_Plane.037_#") {
                  displayNum += "#";
              }
          }
  
          testMesh = GetComponent<TextMesh> ();
          testMesh.text = displayNum;
      }
Thanks a lot its working. I also changed the mouse function to Get$$anonymous$$ouseButtonUp(0) .
Your answer
 
 
              koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                