I am using the following script to display instructions when users hit the “F” key. I am very new to scripting, so this may be a simple question, but when I play my game, the instructions are by default set to active - but I would like them to start the game as inactive, and only become active when the key is hit. Ideas? Thank you!
using UnityEngine;
using System.Collections;
public class onOffInstr : MonoBehaviour {
public Canvas onoffCanvas;
bool CanvasIsOn = false;
void Update()
{
if (Input.GetKeyDown("f"))
{
if (CanvasIsOn == false)
{
CanvasIsOn = true;
onoffCanvas.enabled = CanvasIsOn;
}
else
{
CanvasIsOn = false;
onoffCanvas.enabled = CanvasIsOn;
}
}
}
}
public class OnOffInstr: MonoBehaviour {
public Canvas onOffCanvas;
void Start() {
onOffCanvas.enabled = false;
}
void Update() {
if (Input.GetKeyDown(KeyCode.F)) {
onOffCanvas.enabled = !onOffCanvas.enabled;
}
}
}
Thank you! Would you mind expanding a bit on what was changed? It doesn’t seem to be working as is.
Could be a typo in there, i just typed it up without testing 
Things i changed are:
I added the Start() function to set the initial value of onOffCanvas.enabled to false (so that is isn’t visible normally)
I made the bool obsolete by just toggling the enabled value of onOffCanvas when the key is hit
Personally i dont like to do
Input.GetKeyDown("f");
because i like to use the KeyCode (it is less error prone)
that’s just about it…
what is the error you are getting?
I’m not getting any errors, it’s just not doing anything with I hit F - the canvas is visible at start. Any ideas? I have the script applied to a static object in the scene if that matters.
and you assigned the canvas to the object? (drag it to the inspector)
Got it working, thank you! Not really sure what the issue was, but it seems to be working fine now. 