Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

C# 

(Unity 3D) Text field problem

So I'm messing around trying to create a small game and I've gotten stuck on something very annoying... I've made a SUPER messy script that will allow you to enter your username and then it'll be displayed.. However, I can't figure out how to deselect the textfield after I press enter/return
(I don't want to click outside the textbox with my mouse)

It just creates another line (Which should definitely NOT happen)
I've went through the first 2 pages of google for an answer and NOTHING is working for me......

So someone please help me out so if I press enter it deselects the text field and loads the next level..
(Bonus points if the textfield is automatically activated on start)

Code
using UnityEngine;
using System.Collections;

public class Username : MonoBehaviour {

    public static string name;
    public string stringToEdit = "";

    void OnGUI() {

    
        GUIStyle myCustomStyle = new GUIStyle ();
        GUI.skin.textArea.normal.background = null;
        GUI.skin.textArea.active.background = null;
        GUI.skin.textArea.onHover.background = null;
        GUI.skin.textArea.hover.background = null;
        GUI.skin.textArea.onFocused.background = null;
        GUI.skin.textArea.focused.background = null;
        GUI.skin.textArea.normal.textColor = Color.white;
        stringToEdit = GUI.TextField(new Rect(10, 10, 200, 20), stringToEdit, 9, myCustomStyle);

        
    }
    
    void Update()
    {
        Finished ();
    }

    void Finished()
    {
        if (Input.GetKeyDown ("return"))
        {
            name = stringToEdit;
            Application.LoadLevel(1);
        }
    }

}
Well, from the inspector you can pick the type of the input field, far as I remember there was standard (with single/multi line option)/ password/ PIN/ etc.
And since you're making it with code you'll need to find it in the documentation and see what's the class having it and how to set it.
As for selecting the field on load, you can try the methods mentioned here: http://forum.unity3d.com/threads/focus-o...ly.264472/ just remember to put it in the Awake function to execute once on load only.
Have a cool signature? hit me up and I might use it.


Possibly Related Threads...
Thread / Author Replies Views Last Post
[C#] by SilentkillarHFJan 24, 2018 12:51 PM 1 131 Jan 24, 2018 12:51 PM
Last Post: Dotexe
[C#] by Lexxi.Dec 8, 2016 02:17 AM 3 195 Dec 8, 2016 02:17 AM
Last Post: Dеvil
[C#] by severosNov 18, 2016 02:11 AM 6 236 Nov 18, 2016 02:11 AM
Last Post: clrlslwi