Print text to screen on key-press [JS]

Hey guys,

I am new to programming with JavaScript and I was wondering how you would be able to print text to the screen(whether it be GUI based or what ever) on Key-pressing.

For example, I have this code here for a simple flashlight that allows the player to enable/disable a light (that acts as a flashlight) but what I want to know how to do is make it so that when the player presses the key and enables/disables the light, it relays a message back to the player on their screen whether they turned on the flashlight or turned it off. I know this would be really simple, but I’m trying to learn how to do it and examples would be very helpful for me.

Here is the script:

function Update(){

if (Input.GetKeyDown("f")) {
	if(light.enabled == true)
		light.enabled = false;
		else
		light.enabled = true;
	}
}

Thanks.

You can do the OnGUI on the same script as your flashlight script (provided there’s only one instance of it at any given time). Then check if your light is enabled or not then set the Label (or other GUI stuff you want) accordingly. If you want it to show only for a set amount of time, you’ll want to have a float variable to keep track of the show time then reset the float variable when you turn your flashlight on or off.