Turn on Prefab with Keycode issue
Hello, I have this code where when I press E it turns on a prefab, but issue is when I walk away from the collider an press E anytime it turns on the prefab. So if I walk upto the box an decide not to press E then later on if I press E it turns on a prefab anyway. How do I fix it so if I collide an press E then it turns on Prefab and if I walk way from the box then it wont turn on with E button.
using UnityEngine;
using System.Collections;
public class Turn_On_PREFAB : MonoBehaviour {
public GameObject Prefabtoactivate;
public GameObject locationtospawn;
bool pressed;
bool entered;
// Use this for initialization
void Start () {
pressed = false;
entered = false;
Prefabtoactivate.SetActive (false);
}
void OnTriggerEnter(Collider other)
{
entered = true;
}
void HIT (){
Debug.Log ("HIT");
Prefabtoactivate.SetActive (true);
}
void Update () {
if (Input.GetKey (KeyCode.JoystickButton0) || (Input.GetKey(KeyCode.E)) && entered == true) {
HIT();
}}
}
Comment
Your answer
Follow this Question
Related Questions
Communicate between scenes with prefabs? 3 Answers
How can I make my melee attack hit multiple prefab enemies? 1 Answer
Best practice for triggered sound effects: on prefab, or on a sound object? 0 Answers
Is there a way to filter some colliders out? 2 Answers
Trying to make an event triggered Pendulum Group fall 0 Answers