- Home /
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.
Answer by by0log1c · Mar 07, 2011 at 08:54 PM
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; }
THAN$$anonymous$$ U SOOO $$anonymous$$UCH IT JUST WOR$$anonymous$$ED :) 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 :)
Your answer
Follow this Question
Related Questions
Cloning Problem Script 0 Answers
Cloned interactivecloth not acting like original interactivecloth. 0 Answers
Cloning Objects with Instantiate() - variables/references for added Components not stored? 3 Answers
Instantiated gameObjects can't find each other 0 Answers
How to Parent a Cloned Object to Another Cloned Object 1 Answer