Skip to content

Commit 05a6cb3

Browse files
authored
Added null checks around Controller Manager to handle controllers that connect before the first frame. (#10)
1 parent 4964e9c commit 05a6cb3

File tree

1 file changed

+10
-7
lines changed

1 file changed

+10
-7
lines changed

VR/Assets/Scripts/ControllerManager.cs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,9 @@ public void Initalize()
171171
/// </summary>
172172
public void ClearAll()
173173
{
174+
if(m_Interactors == null)
175+
return;
176+
174177
for(int i = 0; i < (int)ControllerStates.MAX; ++i)
175178
{
176179
m_Interactors[i].Leave();
@@ -184,7 +187,7 @@ public void ClearAll()
184187
/// <param name="parentGamObject">The game object that represents the interactor for that state.</param>
185188
public void SetGameObject(ControllerStates state, GameObject parentGamObject)
186189
{
187-
if (state == ControllerStates.MAX)
190+
if ((state == ControllerStates.MAX) || (m_Interactors == null))
188191
return;
189192

190193
m_Interactors[(int)state].Attach(parentGamObject);
@@ -219,12 +222,6 @@ public void SetState(ControllerStates nextState)
219222

220223
void OnEnable()
221224
{
222-
InputDevices.deviceConnected += RegisterDevices;
223-
List<InputDevice> devices = new List<InputDevice>();
224-
InputDevices.GetDevices(devices);
225-
for (int i = 0; i < devices.Count; i++)
226-
RegisterDevices(devices[i]);
227-
228225
m_RightControllerState.Initalize();
229226
m_LeftControllerState.Initalize();
230227

@@ -236,6 +233,12 @@ void OnEnable()
236233

237234
m_LeftControllerState.ClearAll();
238235
m_RightControllerState.ClearAll();
236+
237+
InputDevices.deviceConnected += RegisterDevices;
238+
List<InputDevice> devices = new List<InputDevice>();
239+
InputDevices.GetDevices(devices);
240+
for (int i = 0; i < devices.Count; i++)
241+
RegisterDevices(devices[i]);
239242
}
240243

241244
void OnDisable()

0 commit comments

Comments
 (0)