- Home /
How to turn off flashlight once in water?
How to put a code to automatically turn off the players flashlight once they touch or enter water. Once they get out of the water the flashlight is still off until they press F again. My flashlight script.
function Update () {
if (Input.GetKeyDown("f")) {
if (light.enabled == true)
light.enabled = false;
else
light.enabled = true;
}
}
Answer by sparkzbarca · Nov 07, 2012 at 12:23 AM
throw a collider on the water. make the collider cover the entrance to the water.
mark the collider as isTrigger
attach this script to the water prefab.
void OnTriggerEnter(collider other)
{
if(other.tag == "Player")
{
other.GameObject.light.enabled = false;
}
}
Not sure if the keywords are exact but thats pretty damn close
Gives errors like insert ";" at the end and some unexpected tokens
well i wrote in C# you wrote in javascript so for example
Void should be function "collider other" should be what "other : collider" I think?
I dont know unitys custom scripting language. But it should be easy enough to translate.
those I think are the only changes needed I believe the if statement should still work.
Your answer
Follow this Question
Related Questions
Flashlight won't turn off. 2 Answers
How to set flashlight to off... 1 Answer
AI enemy Stuck at corners 2 Answers
Turn on/off Script 3 Answers
How to turn on a light by pressing and after few seconds turn it off 1 Answer