- Home /
Function Requires Two Button Presses Instead of One?
I'm working on something with the Oculus Rift as a personal project. Everything has been working fine up until this point. The problem is that with my code, a certain function requires two button presses to complete for some reason. However, I know that the functions are being called the first time, because updates to the UI are made. This portion of code deals with the locking and unlocking of objects in a given position. There are UI Toggles that control the locking of separate objects, and the Y and B buttons lock (and unlock) all objects on the respective hand. The code should read as follows:
If the Y button is pressed, check to see if there are any locked objects in the hand. If so, unlock all objects. Otherwise, if the hand trigger is held to a certain degree while the button is pressed, then lock all objects.
In the unlock function, the objects are sorted through and all are unlocked. Then, a call to a static function in the UI manager is made (and again, this updates the first time the button is pressed). However, both in the inspector and in the game, the objects are shown as still being locked. It requires a second press to unlock the objects. Here's the code.
public void Update(){
///....
if(GetButtonDown(1)){ //Refers to the top button on Oculus Touch controller
if(AnyLocked()){
UnlockObjects();
} else if(GetHandTrigger() >= 0.95f){
LockObjects();
}
}
///....
UpdateObjects();
}
public bool AnyLocked(){
for(int i = 0; i < objs.Length; i++){
if(objs[i].locked)
return true;
}
return false;
}
public void LockObjects(){
for(int i = 0; i < objs.Length; i++){
objs[i].locked = true;
string str = ((leftHand)?"L1":"R1") + "C" + (i+1).ToString();
UIHandler.SetToggleByName(str, false);
}
}
public void UnlockObjects(){
for(int i = 0; i < objs.Length; i++){
objs[i].locked = false;
string str = ((leftHand)?"L1":"R1") + "C" + (i+1).ToString();
UIHandler.SetToggleByName(str, true);
}
}
Is there a logical fallacy I'm missing here? The objects are updated every frame, and putting the UpdateObjects()
function at the end of the locking and unlocking functions does nothing.
Any help would be appreciated!
It would be very helpful to see all the code to walk through all the steps, because based on what you've provided so far it should all function correctly. Seeing the function that gets called twice, and what UpdateObject()
does could point in the right direction
Well, the thing is, the output of the function works the first time, but the locked variable is not changed. I tried placing a Debug.Log in before and after it was changed, and it says the locked variable has been changed, but that change is not shown in the inspector or in-game. $$anonymous$$y guess is the locked variable is changed back somehow before UpdateObjects() is called, but I can't figure out how. The UI should change with the locked variable.
Answer by JSierraAKAMC · Jul 15, 2017 at 01:56 AM
Ah, I forgot that I added an 'On Value Changed' event to the UI toggles, so they'd toggle the objects' lock again after disabling it. Oops.