Search Unity

  1. We've closed the job boards. If you're looking for work, or looking to hire check out Unity Connect. You can see more information here.
    Dismiss Notice
  2. We're running great holiday deals on subscriptions, swag and Asset Store packages! Take a peek at this blog for more information!
    Dismiss Notice
  3. Check out our Unite Austin 2017 YouTube playlist to catch up on what you missed. More videos coming soon.
    Dismiss Notice
  4. Unity 2017.2 is now released.
    Dismiss Notice
  5. The Unity Gear Store is here to help you look great at your next meetup, user group or conference. With all new Unity apparel, stickers and more!
    Dismiss Notice
  6. Introducing the Unity Essentials Packs! Find out more.
    Dismiss Notice
  7. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice
  8. Unity 2017.3 beta is now available for download.
    Dismiss Notice

Simple Input Text Field?

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

  1. GGames

    GGames

    Joined:
    Oct 20, 2012
    Posts:
    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
    Posts:
    31,399
    GUI.TextField.

    --Eric
     
  3. GGames

    GGames

    Joined:
    Oct 20, 2012
    Posts:
    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
    Posts:
    31,399
    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
    Posts:
    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
    Posts:
    66
    Wow, sorry, I forgot to put it inside OnGUI...
     
  7. GGames

    GGames

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

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    31,399
  9. GGames

    GGames

    Joined:
    Oct 20, 2012
    Posts:
    66