- Home /
Help with unresponsive function / if statement
Hey everyone, I'm attempting to make my character pick up an object.
The problem I think I'm having is that the function the code to pickup an item is in (onTriggerStay) seems to run half as often as the update function, and therefore, when the player presses the "Interact" button, approximately half the time, the function isn't running, and nothing is listening for the interact button.
Just to clarify, the code works, but only registers half of my button presses.
function OnTriggerStay(other: Collider)
{
if(Input.GetButtonUp("Interact") && other.gameObject.CompareTag("Pickup"))
{
print("TriggerTest = "+triggerTest++);
}
}
Now, I have tried placing this code before and after the functionUpdate, I've tried using GetButtonDown instead of up, I've tried the control key on my keyboard, and an Xbox controller (Two different input methods).
The reason I think the function is only running half the time, is because if I comment out /Input.GetButtonUp("Interact") && / in the if statement, and add a similar counter into the function update, in the log, the function update tends to count up twice for every one time the OnTriggerStay updates . Does anyone know why this would be happening, or what I could do to further troubleshoot my issue?
I made a work around for this yesterday, but even after sleeping on it, I'm still not happy with the result I have achieved (and can see potential complications arising as the project gets more complex).
Answer by Eric5h5 · Aug 24, 2012 at 04:31 AM
OnTriggerStay runs at the physics framerate (i.e., the same rate as FixedUpdate). You must not use Down or Up events in it, because those events are only true for the single frame in which they are activated, which is fairly likely not to occur during a given physics frame. Down and Up events must only be checked in Update, or an equivalent (such as a coroutine that runs every frame).
Thank you! Although this doesn't help me figure out what to actually do ins$$anonymous$$d. Obviously in the example above, I'm going about it the wrong way, can you recommend a way to achieve what I am attempting to do?
EDIT: I'm accepting this as correct, because it did answer my original question. I'll need to do more research into how to do it so it works, I guess. Thank you again