- Home /
Bool function not running when called from other script
Im making my first Vr game with the HTC Vive. I dont really get the whole ClickedEventArgs thing so i made my own script to check for button-pressed on the controller. but i have some problems with some of the functions. here is the relevant code:
void Update () {
triggerPosition = GetTriggerPos();
}
void LateUpdate(){
tmptriggerpos=triggerPosition;
}
public float GetTriggerPos(){
return controller.GetAxis(Valve.VR.EVRButtonId.k_EButton_SteamVR_Trigger).x;
}
public bool TriggerDown(){
Debug.Log("TriggerDown");
if(tmptriggerpos!=1&&triggerPosition==1){
return true;
}
return false;
}
public bool TriggerUp(){
Debug.Log("TriggerUp");
if(tmptriggerpos==1&&triggerPosition!=1){
return true;
}
return false;
}
public bool TriggerHold(){
if(triggerPosition==1){
return true;
}
return false;
}
Now, if i run any of these functions from the Update
function everything works as intended. But if i run TriggerUp
or TriggerDown
from another script, it doesn't even print. Im pretty positive that the function is not actually called. The TriggerHold
function works perfectly when called from other scripts.
for instance, if i do this in another script it works:
if(controller.TriggerHold()){
Debug.Log("TriggerHeld");
}
but if i do this:
if(controller.TriggerDown()){
Debug.Log("TriggerDown");
}
it doesnt work. why???
Answer by asperix · May 17, 2018 at 01:02 PM
Nevermind! i figured it out. the script was run after the other script. just had to add [DefaultExecutionOrder(-1000)]
at the start
=)
Answer by NoDumbQuestion · May 17, 2018 at 09:27 AM
@asperix Your update:
void LateUpdate(){
tmptriggerpos=triggerPosition;
}
Your If condition
if(tmptriggerpos!=1&&triggerPosition==1){
return true;
}
tmptriggerpos == triggerPosition == 1
It always false
But isnt tmptriggerpos the position the trigger was in last frame? thats why i put it in LateUpdate()
so it was set at the end of the frame, and thus would be the position the trigger was is at the last frame.
LateUpdate
is called after all calls to Update
, but both Update
and LateUpdate
are called every frame.
That's why if you run:
void Update () {
triggerPosition = GetTriggerPos();
}
void LateUpdate(){
tmptriggerpos=triggerPosition;
}
Both triggerPosition
and tmptriggerpos
will be set to the same value from GetTriggerPos()
and will therefore always evaluate to false
in the TriggerUp
function.
Yes, but firstly i set triggerPosition
. then i run all the code, and functions. then, at the end of the script, right before it cuts to next frame, i make a copy og triggerPosition
in tmptriggerpos
. then in the next frame, the first thing i do is set the triggerPosition
to the current position of the trigger, and then i run all the functions and so on. but all of them are run with the current position of the trigger in triggerPosition
, and the position of the trigger i recorded last frame in tmptriggerpos
. am i wrong?
Why dont you add a debug in function to check if both of them really 1 == 1