Pickup Script Problem
Hi!
I have a problem with my pickup script. I can't find anything wrong with my code so some help would be appreciated.
When you go over a trigger tagged, for example "Battery", a text pops up and when you press "E" it should go to your inventory. Everything works except it wont dissapear and go to the inventory. If I delete "if (Input.GetKeyDown(KeyCode.E))" it works fine, but I need that part of the code.
Does anyone know how to fix this little poblem?
var pickupText : UnityEngine.UI.Text;
var flashlight : Flashlight;
var inventoryScript : Inventory;
var pickupSound: AudioClip;
var batteryInRange : boolean;
var flashlightInRange : boolean;
function OnTriggerEnter (other : Collider)
{
if (other.gameObject.CompareTag("Battery"))
{
batteryInRange = true;
if (Input.GetKeyDown(KeyCode.E))
{
inventoryScript.batteries += 1;
other.gameObject.SetActive (false);
}
}
if (other.gameObject.CompareTag("Flashlight"))
{
flashlightInRange = true;
if (Input.GetKeyDown(KeyCode.E))
{
flashlight.enabled = true;
other.gameObject.SetActive (false);
}
}
}
function OnTriggerExit ()
{
batteryInRange = false;
flashlightInRange = false;
}
function Update ()
{
if (batteryInRange)
{
pickupText.text = "[E] PICK UP BATTERY";
}
if (flashlightInRange)
{
pickupText.text = "[E] PICK UP FLASHLIGHT";
}
}
try ins$$anonymous$$d of using setActive, use Destroy().
Answer by spooneystone · Dec 12, 2016 at 05:23 PM
I belive you need to use
"OnTriggerStay" not "OnTriggerEnter", as OnTriggerStay gets called every frame while "other" is within the collider, while OnTriggerEnter only gets called once when it first makes contact with collider.
Your answer
Follow this Question
Related Questions
Error : Unknown identifier : FlashLight 1 Answer
Carrying an object using Javascript 0 Answers
Picking up various types of items? 0 Answers
Inventory Master 0 Answers
RPG Picking up items with polymorphism - probably quick :) 1 Answer