Search Unity

Oculus Quest Integration - SetActive Doesn't work for multiple instantiated prefabs.

Discussion in 'Scripting' started by Lpeis, Oct 19, 2020.

  1. Lpeis

    Lpeis

    Joined:
    Sep 19, 2020
    Posts:
    3
    Hi I am just starting Unity/ Oculus integration.
    I followed a youtube tutorial in adding hand presence in the form of instantiated prefabs

    There is an aspect or two of the SetActive function that I can't seem to get a grasp on.

    The following code works as follows -
    1. Oculus controller is found
    2. Hand prefab is instantiated in its transform.
    3. Hand is turned on and off with SetActive according to the boolean ShowController.
    4. boolean ShowController is toggled by the primarybutton.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using System.Runtime.CompilerServices;
    4. using UnityEngine;
    5. using UnityEngine.XR;
    6.  
    7. public class HandPresence : MonoBehaviour
    8. {
    9.     // Controller Model Prefabs //
    10.     public GameObject handPrefab;
    11.  
    12.     // Input Device //
    13.     private InputDevice targetDevice;
    14.  
    15.     // Instantiated Object Prefabs //
    16.     private GameObject spawnedHand;
    17.  
    18.     // Controller Details //
    19.     public InputDeviceCharacteristics controllerCharacteristic;
    20.     public bool showController = false;
    21.     private bool primaryButtonPressed = false;
    22.  
    23.     void Start()
    24.     {
    25.         // Get Controllers by Device Characteristics //
    26.         List<InputDevice> devices = new List<InputDevice>();
    27.         InputDeviceCharacteristics deviceControllerCharacteristics = controllerCharacteristic | InputDeviceCharacteristics.Controller;
    28.         InputDevices.GetDevicesWithCharacteristics(deviceControllerCharacteristics, devices);
    29.  
    30.         // CHECK FOR DEVICE //
    31.         if (devices.Count > 0)
    32.         {
    33.             targetDevice = devices[0];
    34.  
    35.             if (handPrefab)
    36.             {
    37.                 spawnedHand = Instantiate(handPrefab, transform);
    38.                 if (showController == true)
    39.                 {
    40.                     spawnedHand.SetActive(false);
    41.                 }
    42.                 else if (showController == false)
    43.                 {
    44.                     spawnedHand.SetActive(true);
    45.                 }
    46.             }
    47.         }
    48.     }
    49.  
    50.     void Update()
    51.     {
    52.         // show/ hide according to showController //
    53.         if (showController == true)
    54.         {
    55.             spawnedHand.SetActive(false);
    56.         }
    57.         else if (showController == false)
    58.         {
    59.             spawnedHand.SetActive(true);
    60.         }
    61.  
    62.  
    63.        // If primary button pressed, flip showBoolean //
    64.         if (targetDevice.TryGetFeatureValue(CommonUsages.primaryButton, out bool primaryButtonValue) && primaryButtonValue)
    65.         {
    66.             if (primaryButtonPressed == false)
    67.             {
    68.                 primaryButtonPressed = !primaryButtonPressed;
    69.                 showController = !showController;
    70.             }
    71.         }
    72.         else
    73.         {
    74.             primaryButtonPressed = false;
    75.         }
    76.     }
    77. }
    In this form it works, but I have a couple questions:
    if I edit the void Start() such that the if(handPrefab) statement is missing the SetActive statements,

    Code (CSharp):
    1.   if (handPrefab)
    2.             {
    3.                 spawnedHand = Instantiate(handPrefab, transform);
    4.             }
    the toggling no longer works. In my mind, as long as the SetActive statement are present within void Update(), it should still work as intended.

    Furthermore, even if the SetActive statement is within void Start, but outside the if(handPrefab) statement, the toggling fails. Once again, I'm quite confused by this since the spawnHand GameObject exists outside of the if(handPrefab) statement.

    Code (CSharp):
    1. if (handPrefab)
    2.             {
    3.                 spawnedHand = Instantiate(handPrefab, transform);
    4.              }
    5. if (showController == true)
    6.                 {
    7.                     spawnedHand.SetActive(false);
    8.                 }
    9.                 else if (showController == false)
    10.                 {
    11.                     spawnedHand.SetActive(true);
    12.                 }
    13.             }
    Lastly, instead of just toggling a hand model on/ off, i tried to toggle between a hand prefab and a Oculus controller prefab. I duplicated the code that worked for the handPrefab, but once built and run, the hand continues to toggle, while the controller prefab is constantly active.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using System.Runtime.CompilerServices;
    4. using UnityEngine;
    5. using UnityEngine.XR;
    6.  
    7. public class HandPresence : MonoBehaviour
    8. {
    9.     // Controller Model Prefabs //
    10.     public GameObject handPrefab;
    11.     public GameObject controllerPrefab;
    12.  
    13.     // Input Device //
    14.     private InputDevice targetDevice;
    15.  
    16.     // Instantiated Object Prefabs //
    17.     private GameObject spawnedHand;
    18.     private GameObject spawnedController;
    19.  
    20.     // Controller Details //
    21.     public InputDeviceCharacteristics controllerCharacteristic;
    22.     public bool showController;
    23.  
    24.     private bool primaryButtonPressed = false;
    25.     private bool triggerPressed = false;
    26.  
    27.     void Start()
    28.     {
    29.         // Get Controllers by Device Characteristics //
    30.         List<InputDevice> devices = new List<InputDevice>();
    31.         InputDeviceCharacteristics deviceControllerCharacteristics = controllerCharacteristic | InputDeviceCharacteristics.Controller;
    32.         InputDevices.GetDevicesWithCharacteristics(deviceControllerCharacteristics, devices);
    33.  
    34.         // CHECK FOR DEVICE //
    35.         if (devices.Count > 0)
    36.         {
    37.             targetDevice = devices[0];
    38.  
    39.             // IF HAVE MODEL PREFAB //
    40.  
    41.             if (handPrefab)
    42.             {
    43.                 spawnedHand = Instantiate(handPrefab, transform);
    44.                 if (showController == true)
    45.                 {
    46.                     spawnedHand.SetActive(false);
    47.                 }
    48.                 else if (showController == false)
    49.                 {
    50.                     spawnedHand.SetActive(true);
    51.                 }
    52.             }
    53.  
    54.             if (controllerPrefab)
    55.             {
    56.                 spawnedController = Instantiate(controllerPrefab, transform);
    57.                 if (showController == true)
    58.                 {
    59.                     spawnedController.SetActive(true);
    60.                 }
    61.                 else if (showController == false)
    62.                 {
    63.                     spawnedController.SetActive(false);
    64.                 }
    65.             }
    66.         }
    67.     }
    68.  
    69.     void Update()
    70.     {
    71.         // hide/ show according to showController //
    72.         if (showController == true)
    73.         {
    74.             spawnedController.SetActive(true);
    75.             spawnedHand.SetActive(false);
    76.         }
    77.         else if (showController == false)
    78.         {
    79.             spawnedController.SetActive(false);
    80.             spawnedHand.SetActive(true);
    81.         }
    82.  
    83.        // If primary button pressed, flip showBoolean //
    84.         if (targetDevice.TryGetFeatureValue(CommonUsages.primaryButton, out bool primaryButtonValue) && primaryButtonValue)
    85.         {
    86.             if (primaryButtonPressed == false)
    87.             {
    88.                 primaryButtonPressed = !primaryButtonPressed;
    89.                 showController = !showController;
    90.             }
    91.         }
    92.         else
    93.         {
    94.             primaryButtonPressed = false;
    95.         }
    96.     }
    97. }
    98.  
    99.  
    I'm pretty certain its code related and not model related.
    I don't believe I have any other scripts hidden elsewhere within the scene.

    Thanks!
     
    Last edited: Oct 19, 2020