1. We've introduced thread tags, search within a thread and similar thread search. Read more here.
    Dismiss Notice
  2. We've released our first Timeline Experimental Preview, our new tool for creating cutscenes and more! To check it out click here.
    Dismiss Notice
  3. All Unity Pro perpetual license customers: your special offer to subscribe is ready! Click here.
    Dismiss Notice
  4. Unity 5.5 is now released.
    Dismiss Notice
  5. Check out all the fixes for 5.5 in patch releases 1 & 2.
    Dismiss Notice
  6. Unity 5.6 beta is now available for download.
    Dismiss Notice
  7. Get prepared for the Tizen Mobile App Incentive Program! Read more about the upcoming program here.
    Dismiss Notice
  8. Enter the Microsoft Developer Challenge for a chance to win prizes. Read more about it here.
    Dismiss Notice

Simple Input Text Field?

Discussion in 'Scripting' started by GGames, Jul 26, 2013.

  1. GGames

    GGames

    Joined:
    Oct 20, 2012
    Messages:
    66
    I need a simple input TextField in Unity, for example for writing the user's name.
    Something like the Input Text Fields in Flash would be perfect.
    When the users clicks on them typing is enabled.
     
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Messages:
    30,886
    GUI.TextField.

    --Eric
     
  3. GGames

    GGames

    Joined:
    Oct 20, 2012
    Messages:
    66
    Can you provide example code?
    GUI.TextField(new Rect(32, 32, 180, 32), "xd");
    This gives me an error.
     
  4. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Messages:
    30,886
    The docs have sample code. You really need to say what the error is; "this give me an error" doesn't help much.

    --Eric
     
  5. GGames

    GGames

    Joined:
    Oct 20, 2012
    Messages:
    66
    The exact code I posted gives me the following error:
    "NullReferenceException: Object reference not set to an instance of an object
    UnityEngine.GUIUtility.GetControlID (FocusType focus, Rect position)
    UnityEngine.GUI.TextField (Rect position, System.String text)
    Main.Start () (at Assets/Main.cs:12)
    "
     
  6. GGames

    GGames

    Joined:
    Oct 20, 2012
    Messages:
    66
    Wow, sorry, I forgot to put it inside OnGUI...
     
  7. GGames

    GGames

    Joined:
    Oct 20, 2012
    Messages:
    66
    Also, any idea how to restrict the input to only numbers?
     
  8. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Messages:
    30,886
  9. GGames

    GGames

    Joined:
    Oct 20, 2012
    Messages:
    66