OnTriggerStay Instantiate Problem (just want a single spawn, but there are multiple)

Hello People, im trying to spawn a collectable gameobjet every time my Character Stays within a Trigger and presses a button.

e.g. Move Player in front of an automat, press "E" and spawn a collectable ticket besides the automat. Thats the EXACT way i want it to be happen.

i got it completly working so far with this script:

var object: GameObject;
var object2: GameObject;

function OnTriggerStay(Player : Collider) {
    if (Input.GetButtonUp("Action")){
        Instantiate (object, Vector3(11,1,16), transform.rotation);
        Debug.Log("You can pick up your ticket right next to the vendor.");
    }
    else
    {
        if (Input.GetKeyUp("r")){
            Instantiate (object2, Vector3(13.4,1,16), transform.rotation);
            Debug.Log("You can pick up your ticket right next to the vendor.");
        }
    }
}

BUT every time i Press the "E" button to instantiate the ticket it instantiates MULTIPLE tickets. i just want a single one tho. Could anyone help me out with this pls ?

thx in advance :)

You don't need to write "solved" in the title; that's what accepting answers is for.

1 Answer

1

Using OnTriggerStay is something I try to avoid as much as possible. Try setting a boolean according to whether your character is close or not to the machine. Something like this:

   var object: GameObject;
   var object2: GameObject;
   var isNearby:boolean;

function Update(){
    if(Input.GetButtonUp("Action") && isNearby){
        Instantiate (object, Vector3(11,1,16), transform.rotation);
        Debug.Log("You can pick up your ticket right next to the vendor.");
    }else{
        if(Input.GetKeyUp("r") && isNearby){
            Instantiate (object2, Vector3(13.4,1,16), transform.rotation);
            Debug.Log("You can pick up your ticket right next to the vendor.");
        }
    }
}

function OnTriggerEnter(Player : Collider) {
    isNearby = true;
}

function OnTriggerExit(Player : Collider) {
    isNearby = false;
}

THANK U SOOO MUCH IT JUST WORKED :) Thank you i tried to figure it out myself since 3 days XD

Glad I could help :) Not trying to pump up my reputation but the norm is usually to vote the helping answer up rather than just add 'Solved' in the thread name, so the website deals with it accordingly :)

alright :) didnt know that thx for advice