- Home /
Can't use KeyCode multiple times
Im making a light switch code, so when I go the the trigger the text pops up (Press E to use) and when I press E then the light turns on but I cant use E again in the (else if) I have to use other key code to make it work. Here is the script, thanks
using UnityEngine;
using System.Collections;
public class LightSwitch : MonoBehaviour {
public GameObject light;
public bool activateTrigger = false;
public bool T_ActivatedOpen = true;
public bool T_ActivatedClose = false;
public bool Press = false;
public GameObject textO;
public GameObject textC;
void Start () {
textC.SetActive(false);
textO.SetActive(false);
T_ActivatedOpen = true;
T_ActivatedClose = false;
light.SetActive(false);
}
void Update () {
if (T_ActivatedOpen == true)
T_ActivatedClose = false;
else if (T_ActivatedClose == true)
T_ActivatedOpen = false;
if (activateTrigger && Input.GetKey(KeyCode.E))
{
textO.SetActive(false);
textC.SetActive(false);
T_ActivatedOpen = false;
T_ActivatedClose = true;
textO.SetActive(false);
textC.SetActive(true);
light.SetActive(true);
if (light == true)
textC.SetActive(true);
textO.SetActive(false);
}
else if (activateTrigger && Input.GetKey(KeyCode.F))
{
light.SetActive(false);
textO.SetActive(true);
textC.SetActive(false);
if (light == false)
textC.SetActive(false);
textO.SetActive(true);
}
}
void OnTriggerEnter(Collider col)
{
if (col.gameObject.tag == "Player")
{
activateTrigger = true;
if ((T_ActivatedOpen == true))
textO.SetActive(true);
if ((T_ActivatedClose == true))
textC.SetActive(true);
}
}
void OnTriggerExit(Collider col)
{
if (col.gameObject.tag == "Player")
{
textO.SetActive(false);
textC.SetActive(false);
activateTrigger = false;
}
}
}
Answer by Bunny83 · Nov 25, 2016 at 08:48 AM
Uhh, this is pure chaos ^^ I think you fiddled too long on this script. You have tons of redundancy and nonsense all over the place.
Here, i rewrote the script:
using UnityEngine;
using System.Collections;
public class LightSwitch : MonoBehaviour
{
public GameObject light;
public GameObject textO;
public GameObject textC;
void Start()
{
textC.SetActive(false);
textO.SetActive(false);
}
void OnTriggerEnter(Collider col)
{
if (col.gameObject.tag == "Player")
{
textO.SetActive(light.activeSelf);
textC.SetActive(!light.activeSelf);
}
}
void OnTriggerExit(Collider col)
{
if (col.gameObject.tag == "Player")
{
textO.SetActive(false);
textC.SetActive(false);
}
}
void OnTriggerStay(Collider col)
{
if (col.gameObject.tag == "Player")
{
if (Input.GetKeyDown(KeyCode.E))
{
// toggle the light. If off turn it on, if on turn it off
light.SetActive(!light.activeSelf);
// update the texts based on the new active state.
textO.SetActive(light.activeSelf);
textC.SetActive(!light.activeSelf);
}
}
}
}
Major changes:
OnTriggerEnter is called every frame while something is inside the trigger. So we don't need Update at all
I used GetKeyDown instead of GetKey. GetKeyDown only returns true for one frame when you press the key down. GetKey returns the "state" of the key so when you hold the key down it will return true every frame.
No need for all those variables. The light gameobject has it's active state which you want to toggle in the first place, so you can simply read it back when you want to know the current state.
Your answer
Follow this Question
Related Questions
Need Help Improving If Else statement 1 Answer
"Else" not working 2 Answers
How to use multiple else if statements C# 2 Answers
VertexLit particles not affected on all directions 0 Answers
Why do Unity 5 is glitched as hell? Please, I need help... 0 Answers