This is what I tried so far, the first function works, the second doesn’t
using UnityEngine;
using System.Collections;
public class Flashlight_Script : MonoBehaviour {
bool lightbutto=true;
public Light flashlight;
void FixedUpdate () {
if(Input.GetKey(KeyCode.R)&&(flashlight.enabled=true)){
flashlight.enabled=false;
}
if(Input.GetKey(KeyCode.R)&&(flashlight.enabled=false)){
flashlight.enabled=true;
}
}
}
When you say first and second function do you mean this?
if(Input.GetKey(KeyCode.R)&&(flashlight.enabled=true)){
flashlight.enabled=false;
}
if(Input.GetKey(KeyCode.R)&&(flashlight.enabled=false)){
flashlight.enabled=true;
}
They aren’t functions, they are if statements. This is a function.
void FixedUpdate () {
if(Input.GetKey(KeyCode.R)&&(flashlight.enabled=true)){
flashlight.enabled=false;
}
if(Input.GetKey(KeyCode.R)&&(flashlight.enabled=false)){
flashlight.enabled=true;
}
}
Your immediate problem is as follows:
if(Input.GetKey(KeyCode.R)&&(flashlight.enabled=true)){
If you want to compare something to something else you need to use “==” not “=” try
if(Input.GetKey(KeyCode.R)&&(flashlight.enabled==true)){
and
if(Input.GetKey(KeyCode.R)&&(flashlight.enabled==false)){
once you have done that you’ll probably find that your light flickers on and off. This is because you have used
Input.GetKey(KeyCode.R)
What this does is return true if a key is held down. What you want is for the torch to come on/go off when ever the key is pressed down initially or when the key is released. Try
Input.GetKeyUp(KeyCode.R)