- Home /
Wy does this destroy script not work?
var YellowFlower : int;
var YellowFlowerPick = false;
function OnTriggerEnter(other : Collider){
if(other.tag == "YellowFlower"){
YellowFlowerPick = true;
if(Input.GetButtonDown("E")){
print("Destroy");
Destroy(other.gameObject);
}
}
}
function Update(){
if(YellowFlowerPick == true){
if(Input.GetButtonDown("E")){
YellowFlower += 1;
}
}
}
I can Pick up the flowers(the YellowFlower int goes up) but I´d wont destroy it self! I don´t even get the print message! If anyone could help me and tell me what I´ve done wrong.. (I have added E to the Inputs)
Answer by Bunny83 · Dec 16, 2012 at 02:17 PM
OnTriggerEnter will be called in the frame when you enter the trigger. This is only called one time. GetButtonDown also returns true only in the frame where you pressed the button down. It's almost impossible to enter the trigger and press the button in the exact same frame.
I guess you want something like that:
var YellowFlower : int;
var YellowFlowerPick : GameObject = null;
function OnTriggerEnter(other : Collider)
{
if(other.tag == "YellowFlower"){
YellowFlowerPick = other.gameObject;
}
}
function OnTriggerExit(other : Collider)
{
if(other.tag == "YellowFlower"){
YellowFlowerPick = null;
}
}
function Update()
{
if(YellowFlowerPick != null){
if(Input.GetButtonDown("E")){
YellowFlower += 1;
Destroy(YellowFlowerPick);
}
}
}
Answer by XienDev · Dec 16, 2012 at 02:17 PM
Are u sure than ontrigger enter works, seccond.. OnTrigger enter is called once, it's hard to press button on this event, use Input.GetButton, or better to use onTriggerStay fo check in input in your sample, or use input.getKey if u want =)