- Home /
Pressing a button to make chat appear and disappear
public bool useChat = true;
if(useChat){
if(Input.GetKeyDown(KeyCode.Return)){
if(viewChat) {
viewChat=true;
print("true");
} else {
viewChat=false;
print("false");
}
}
Im trying to make it so when I press enter it shows the chat and if I press enter again the chat disappears. Right now when I press enter. Nothing shows up but in the console it says both true and false
FYI You can do this in C#: viewChat = !viewChat that is an easy and cant be written wrong if you just want to switch a bool value :)
Answer by Lazdude17 · Jul 29, 2014 at 04:26 PM
You are only setting viewChat to true if viewChat is true... Is there some other code that also affects the viewChat variable?
I think what you want is
if(Input.GetKeyDown(KeyCode.Return)) //If you hit enter
{
if(viewChat) //And if viewChat is true, meaning chat is open
{
viewChat = false; //Then close the chat
}
else
{
viewChat = true; //Otherwise turn chat on if the opposite is true
}
print(viewChat); //Show me what viewChat is every time I press enter
}
It still doesn't show. But when I change Get$$anonymous$$eyDown to Get$$anonymous$$ey When I hold enter, the chatbox shows
Are you running this under the Update() function? You should always check for input in the Update() function.
Answer by feyyd · Jul 29, 2014 at 04:39 PM
Snippet will toggle viewChat when Return is pressed, print true or false.
if( Input.GetKeyDown(KeyCode.Return) )
{
viewChat = !viewChat;
if ( viewChat ) Debug.Log ("true");
else Debug.Log ("false");
}