0
\$\begingroup\$

I have in my hierarchy tab a Canvas. Inside the canvas I have an Input Field that holds a placeholder and a text.

I read the documentations and didn't find how to hide the Input field.

I want that if a if statement is true, then it will show the input field. Else you won't see it.

How can I implement it?


EDIT

I thought about an idea to test: If I press the O key on the keyboard it disappears, and if I press again it appears; and so on.

This is what I tried:

bool shown = true;
// Use this for initialization
void Start () {

}

// Update is called once per frame
void Update () {
    GameObject inputField = GameObject.Find("InputField");
    if (Input.GetKeyDown (KeyCode.O)) {
        shown = !shown;
        inputField.SetActive (shown);
    }
}

I play the scene and when I press the O key I get

NullReferenceException: Object reference not set to an instance of an object

If I double-click on the console message it highlights me the if (Input.GetKeyDown (KeyCode.O)) { line.

Something not good does the SetActive(bool) method, I suppose.

\$\endgroup\$
  • \$\begingroup\$ The canvas tag is for HTML5 only, please don't use it for unity \$\endgroup\$ – Bálint Sep 29 '16 at 18:03
2
\$\begingroup\$

You can reference the InputField as a GameObject and call the SetActive(false) method on it to turn it off. You can then use your if statement, to turn the same GameObject on.

public class SomeClass : MonoBehaviour {
    public GameObject inputField;

    void SomeMethod(){
        if(someCondition)
            inputField.SetActive(false);
    }
}

Set the GameObject in the inspector by drag drop, or use GameObject.Find in awake to set inputField

\$\endgroup\$
  • \$\begingroup\$ It doesn't work, don't know why... I thought about an idea to prove: If I press the O key on the keyboard it disappears, and if I press again it appears; and so on. \$\endgroup\$ – Pichi Wuana Oct 3 '16 at 10:18
  • \$\begingroup\$ First of all, I see you're doing GameObject.Find in the Update loop. This is a very bad idea, as it will try to find the GameObject every frame. Instead just cache it in the start. Declare a global variable inside the class named input field, and then do the GameObject.Find in start. The rest of the code is fine. As for the nullreference, you need to pass the same name as the inputfield's GameObject as you have in the scene. So it should be named InputField with exact same case sensitivity. And you can't find deactivated GameObjects, so start the scene with it being active. \$\endgroup\$ – Shraa1 Oct 3 '16 at 13:36
  • \$\begingroup\$ If you want, deactivate it in start \$\endgroup\$ – Shraa1 Oct 3 '16 at 13:39
2
\$\begingroup\$

You can create a public GameObject variable and then disable/enable it accordingly:

public GameObject field; //  On Editor drag the inputfield GameObject here 
...

...
if (*condition*) {
    field.SetActive(true);
} else {
    field.SetActive(false);
}

For the second question you made: Change the GameObject inputField = GameObject.Find("InputField");It should not be on the update. See this solution:

bool shown = true;
GameObject inputField;
// Use this for initialization
void Start () {
    inputField  = GameObject.Find("InputField");
}

// Update is called once per frame
void Update () {
    if (Input.GetKeyDown (KeyCode.O)) {
        shown = !shown;
        inputField.SetActive (shown);
    }
}

You can also use ,as I said in the first place, a public GameObject. Then on the unity editor you can drag the InputField GameObject to the Variable.

\$\endgroup\$
  • \$\begingroup\$ You can also navigate the Transform hierarchy to locate the relevant object (for staticly-built UIs this is not recommended). \$\endgroup\$ – Draco18s Sep 29 '16 at 18:17
  • \$\begingroup\$ Very simple! You have to change the: GameObject inputField = GameObject.Find("InputField"); from the Update to the Start. See the answer \$\endgroup\$ – Trigueiro Oct 3 '16 at 19:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.