Game Development Stack Exchange is a question and answer site for professional and independent game developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

share|improve this question
    
The canvas tag is for HTML5 only, please don't use it for unity – Bálint Sep 29 '16 at 18:03
up vote 2 down vote accepted

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.

Edit:

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

share|improve this answer
1  
Maybe add some code to show how it's done. – Lasse Sep 29 '16 at 15:53
    
I've added a small example. If you're still not clear, I'd be glad to help you out with any other questions you may have – Shraa1 Sep 29 '16 at 18:09
    
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. – Pichi Wuana Oct 3 '16 at 10:18
    
See my edit, thanks! – Pichi Wuana Oct 3 '16 at 10:25
1  
It worked! Thanks! – Pichi Wuana Oct 9 '16 at 12:59

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.

share|improve this answer
    
You can also navigate the Transform hierarchy to locate the relevant object (for staticly-built UIs this is not recommended). – Draco18s Sep 29 '16 at 18:17
    
See my edit, thanks! – Pichi Wuana Oct 3 '16 at 10:25
    
Very simple! You have to change the: GameObject inputField = GameObject.Find("InputField"); from the Update to the Start. See the answer – Trigueiro Oct 3 '16 at 19:07

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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