@@ -127,6 +127,7 @@ public override uint GetControllerIndex(GameObject controller)
127
127
/// <returns>The GameObject of the controller</returns>
128
128
public override GameObject GetControllerByIndex ( uint index , bool actual = false )
129
129
{
130
+ SetupPlayer ( ) ;
130
131
VRTK_SDKManager sdkManager = VRTK_SDKManager . instance ;
131
132
switch ( index )
132
133
{
@@ -146,7 +147,7 @@ public override GameObject GetControllerByIndex(uint index, bool actual = false)
146
147
/// <returns>A Transform containing the origin of the controller.</returns>
147
148
public override Transform GetControllerOrigin ( VRTK_ControllerReference controllerReference )
148
149
{
149
- return controllerReference . actual . transform ;
150
+ return ( controllerReference != null && controllerReference . actual != null ? controllerReference . actual . transform : null ) ;
150
151
}
151
152
152
153
/// <summary>
@@ -326,6 +327,7 @@ public override SDK_ControllerHapticModifiers GetHapticModifiers()
326
327
/// <returns>A Vector3 containing the current velocity of the tracked object.</returns>
327
328
public override Vector3 GetVelocity ( VRTK_ControllerReference controllerReference )
328
329
{
330
+ SetupPlayer ( ) ;
329
331
uint index = VRTK_ControllerReference . GetRealIndex ( controllerReference ) ;
330
332
switch ( index )
331
333
{
@@ -345,6 +347,7 @@ public override Vector3 GetVelocity(VRTK_ControllerReference controllerReference
345
347
/// <returns>A Vector3 containing the current angular velocity of the tracked object.</returns>
346
348
public override Vector3 GetAngularVelocity ( VRTK_ControllerReference controllerReference )
347
349
{
350
+ SetupPlayer ( ) ;
348
351
uint index = VRTK_ControllerReference . GetRealIndex ( controllerReference ) ;
349
352
switch ( index )
350
353
{
@@ -423,11 +426,19 @@ public override bool GetControllerButtonState(ButtonTypes buttonType, ButtonPres
423
426
424
427
protected virtual void OnEnable ( )
425
428
{
426
- GameObject simPlayer = SDK_InputSimulator . FindInScene ( ) ;
427
- if ( simPlayer != null )
429
+ SetupPlayer ( ) ;
430
+ }
431
+
432
+ protected virtual void SetupPlayer ( )
433
+ {
434
+ if ( rightController == null || leftController == null )
428
435
{
429
- rightController = simPlayer . transform . Find ( RIGHT_HAND_CONTROLLER_NAME ) . GetComponent < SDK_ControllerSim > ( ) ;
430
- leftController = simPlayer . transform . Find ( LEFT_HAND_CONTROLLER_NAME ) . GetComponent < SDK_ControllerSim > ( ) ;
436
+ GameObject simPlayer = SDK_InputSimulator . FindInScene ( ) ;
437
+ if ( simPlayer != null )
438
+ {
439
+ rightController = ( rightController == null ? simPlayer . transform . Find ( RIGHT_HAND_CONTROLLER_NAME ) . GetComponent < SDK_ControllerSim > ( ) : rightController ) ;
440
+ leftController = ( leftController == null ? simPlayer . transform . Find ( LEFT_HAND_CONTROLLER_NAME ) . GetComponent < SDK_ControllerSim > ( ) : leftController ) ;
441
+ }
431
442
}
432
443
}
433
444
@@ -521,21 +532,22 @@ protected virtual bool GetControllerButtonState(uint index, string keyMapping, B
521
532
/// <returns>Returns true if the button is being pressed.</returns>
522
533
protected virtual bool IsButtonPressed ( uint index , ButtonPressTypes type , KeyCode button )
523
534
{
535
+ SetupPlayer ( ) ;
524
536
if ( index >= uint . MaxValue )
525
537
{
526
538
return false ;
527
539
}
528
540
529
541
if ( index == 1 )
530
542
{
531
- if ( rightController != null && ! rightController . Selected )
543
+ if ( ! rightController . Selected )
532
544
{
533
545
return false ;
534
546
}
535
547
}
536
548
else if ( index == 2 )
537
549
{
538
- if ( leftController != null && ! leftController . Selected )
550
+ if ( ! leftController . Selected )
539
551
{
540
552
return false ;
541
553
}
0 commit comments