Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Is it possible to display textfield when button is click?

Discussion in 'Scripting' started by jerex, Aug 28, 2014.

  1. jerex

    jerex

    Joined:
    Jun 21, 2014
    Posts:
    28
    Hi guys. Is it possible to display the textfield when the button is click? i want that my scene is hiding the textfield when it is played then when the button is click, that's the time the textfield will appear. where will i put the code if it is possible? here is my code. just edit it if you want. :)
    Code (CSharp):
    1. using System;
    2. using UnityEngine;
    3. using System.Text.RegularExpressions;
    4. using System.Collections;
    5.  
    6. public class bedA : MonoBehaviour {
    7.    
    8.     public static string kamaA="";
    9.     public Texture background;
    10.     public GUIStyle Next;
    11.     public GUIStyle Back;
    12.     public GUIStyle Yes;
    13.     public GUIStyle upuan_style;
    14.     public float guiPlacementX1;
    15.     public float guiPlacementY1;
    16.     public float guiPlacementX2;
    17.     public float guiPlacementY2;
    18.     public float guiPlacementX3;
    19.     public float guiPlacementY3;
    20.    
    21.     void OnGUI(){
    22.         GUI.DrawTexture (new Rect (0, 0, Screen.width, Screen.height), background);
    23.  
    24.         kamaA = GUI.TextField (new Rect (318, 295, 240 , 45), kamaA);
    25.         kamaA = Regex.Replace (kamaA, "[^0-9]", "");
    26.    
    27.         if(GUI.Button(new Rect(Screen.width* guiPlacementX1, Screen.height*guiPlacementY1, Screen.width*.2f, Screen.height*.15f),"", Back)){
    28.             Application.LoadLevel("Choice1");  
    29.         }
    30.        
    31.         if(CheckInput(kamaA))
    32.         {
    33.             if(GUI.Button(new Rect(Screen.width* guiPlacementX2, Screen.height*guiPlacementY2, Screen.width*.2f, Screen.height*.15f),"", Next)){
    34.                 Application.LoadLevel("5bedB");  
    35.             }
    36.         }
    37.     }
    38.     bool CheckInput(string data)
    39.     {
    40.         bool result = false;
    41.        
    42.         int convertedData = 0;
    43.         if(Int32.TryParse(data, out convertedData))
    44.         {
    45.             if(convertedData >= 0 & convertedData <= 2)
    46.                 result = true;
    47.         }
    48.         return result;
    49.     }
    50. }
    51.  
     
  2. Limeoats

    Limeoats

    Joined:
    Aug 6, 2014
    Posts:
    104
    Not related to your actual question, but a bit of advice...

    The if-statement on line 45 is not doing what you think it is.

    I'm assuming you want to make sure that the number is 0, 1, or 2.
    You are going to need to use && for that comparison.

    By using a single &, you are computing the logical AND of the two sides of the operation. It will return true or false depending on whether or not the two sides of the equation are true or false.

    Be very careful when doing comparisons. Unless you are very good with bit-wise operations, you will most likely want to use && for your comparisons.
     
    laurelhach likes this.
  3. jerex

    jerex

    Joined:
    Jun 21, 2014
    Posts:
    28
    there is no difference on it. try to check it. :)
     
  4. Limeoats

    Limeoats

    Joined:
    Aug 6, 2014
    Posts:
    104
    There may be no difference in this exact example, but you might not always get the results you are expecting.

    I wasn't sure if you did that intentionally or it's just one of those cases where you accidentally only put one & and it works by coincidence so you don't notice.
     
    Polymorphik likes this.
  5. Fraconte

    Fraconte

    Joined:
    Dec 6, 2013
    Posts:
    327
    Code (CSharp):
    1.  
    2. if (GUI.Button())
    3. {
    4.     GUI.Text();
    5.     ...all the code you want when the button is pressed
    6. }
    7.  
     
  6. Polymorphik

    Polymorphik

    Joined:
    Jul 25, 2014
    Posts:
    599
    That will only happen in one frame.

    Do you want the text field to persist? And then go away after X amount of time?