Search Unity

  1. We are migrating the Unity Forums to Unity Discussions. On July 12, the Unity Forums will become read-only. On July 15, Unity Discussions will become read-only until July 18, when the new design and the migrated forum contents will go live. Read our full announcement for more information and let us know if you have any questions.

Resolved Input system not working in build

Discussion in 'Editor & General Support' started by jlorenzi, Mar 18, 2023.

  1. jlorenzi

    jlorenzi

    Joined:
    May 2, 2021
    Posts:
    328
    EDIT: The reason why it wasn't working was because in the script for getting inputs it tries to get the value of "Swap Controls" but in the build for the quest there was no save file for that, so it would get a null reference error.

    Here's a really weird problem. I'm making a VR game for the quest 2, and when I test it on my computer everything works but when I build it and upload it to my quest 2 and test it there, it doesn't detect any input from my controllers. I'm certain this is a problem with getting the inputs and not the scripts using the inputs because the code for the tutorial is this
    Code (CSharp):
    1.     private void Update()
    2.     {
    3.         if ( done )
    4.         {
    5.             ES3.Save("Finished Tutorial", true);
    6.             return;
    7.         }
    8.  
    9.         if ( index == 0 && Inputs.leftTrigger > 0.8f || index == 0 && Inputs.rightTrigger > 0.8f )
    10.         {
    11.             index++;
    12.         }
    13.  
    14.         if ( index == 1 && Inputs.aButtonPressed )
    15.         {
    16.             index++;
    17.         }
    18.  
    19.         if ( index == 2 && Inputs.bButtonPressed )
    20.         {
    21.             index++;
    22.         }
    23.  
    24.         if ( index == 3 && Inputs.bButtonPressed && !jump.isGrounded )
    25.         {
    26.             index++;
    27.  
    28.             swordCrate.SetActive(true);
    29.         }
    30.  
    31.         if ( index == 4 && Inputs.playerInteracted )
    32.         {
    33.             index++;
    34.         }
    35.  
    36.         if ( index == 5 )
    37.         {
    38.             if ( Inputs.leftGrip > 0.8f )
    39.             {
    40.                 index++;
    41.             }
    42.  
    43.             else if ( Inputs.rightGrip > 0.8f )
    44.             {
    45.                 index++;
    46.             }
    47.         }
    48.  
    49.         if ( index > 0 )
    50.         {
    51.             texts[index - 1].SetActive(false);
    52.         }
    53.  
    54.         texts[index].SetActive(true);
    55.      
    56.         if ( index == 6 )
    57.         {
    58.             StartCoroutine(End());
    59.         }
    60.     }
    I don't see any way for this to go wrong in the build.

    Also, previous builds worked completely fine. This error only started happening after I made a new scene for a 3rd map in the game. I can't remember changing anything else.

    Here's the code for getting the inputs
    Code (CSharp):
    1. using System.Collections.Generic;
    2. using UnityEngine;
    3. using UnityEngine.XR;
    4. using UnityEngine.XR.Interaction.Toolkit;
    5.  
    6. [System.Serializable]
    7. public class GetInputs : MonoBehaviour
    8. {
    9.     public InputDeviceCharacteristics controller;
    10.     public bool isLeftController;
    11.     public XRInteractorLineVisual xrRay;
    12.  
    13.     private InputDevice targetDevice;
    14.     private bool UISelected;
    15.  
    16.     private void Start()
    17.     {
    18.         GetDevice();
    19.     }
    20.  
    21.     private void Update()
    22.     {
    23.         if ( xrRay.reticle.activeInHierarchy )
    24.         {
    25.             Inputs.leftTrigger = 0;
    26.             Inputs.rightTrigger = 0;
    27.          
    28.             UISelected = true;
    29.         }
    30.  
    31.         else
    32.         {
    33.             UISelected = false;
    34.         }
    35.  
    36.         if ( !targetDevice.isValid )
    37.         {
    38.             GetDevice();
    39.  
    40.             return;
    41.         }
    42.  
    43.         if ( targetDevice.TryGetFeatureValue(CommonUsages.trigger, out float t) )
    44.         {
    45.             if ( UISelected )
    46.             {
    47.                 return;
    48.             }
    49.  
    50.             if ( isLeftController )
    51.             {
    52.                 if ( ES3.Load<bool>("Swap Controls") )
    53.                 {
    54.                     Inputs.rightTrigger = t;
    55.                 }
    56.  
    57.                 else
    58.                 {
    59.                     Inputs.leftTrigger = t;
    60.                 }
    61.             }
    62.  
    63.             else
    64.             {
    65.                 if ( ES3.Load<bool>("Swap Controls") )
    66.                 {
    67.                     Inputs.leftTrigger = t;
    68.                 }
    69.  
    70.                 else
    71.                 {
    72.                     Inputs.rightTrigger = t;
    73.                 }
    74.             }
    75.         }
    76.  
    77.         if ( targetDevice.TryGetFeatureValue(CommonUsages.grip, out float g) )
    78.         {
    79.             if ( isLeftController )
    80.             {
    81.                 if ( ES3.Load<bool>("Swap Controls") )
    82.                 {
    83.                     Inputs.rightGrip = g;
    84.                 }
    85.  
    86.                 else
    87.                 {
    88.                     Inputs.leftGrip = g;
    89.                 }
    90.             }
    91.  
    92.             else
    93.             {
    94.                 if ( ES3.Load<bool>("Swap Controls") )
    95.                 {
    96.                     Inputs.leftGrip = g;
    97.                 }
    98.  
    99.                 else
    100.                 {
    101.                     Inputs.rightGrip = g;
    102.                 }
    103.             }
    104.         }
    105.  
    106.         if ( targetDevice.TryGetFeatureValue(CommonUsages.primaryButton, out bool b) )
    107.         {
    108.             if ( isLeftController )
    109.             {
    110.                 Inputs.xButtonPressed = b;
    111.             }
    112.  
    113.             else
    114.             {
    115.                 Inputs.aButtonPressed = b;
    116.             }
    117.         }
    118.      
    119.         if ( targetDevice.TryGetFeatureValue(CommonUsages.secondaryButton, out bool n) )
    120.         {
    121.             if ( isLeftController )
    122.             {
    123.                 Inputs.yButtonPressed = n;
    124.             }
    125.  
    126.             else
    127.             {
    128.                 Inputs.bButtonPressed = n;
    129.             }
    130.         }
    131.  
    132.         if ( targetDevice.TryGetFeatureValue(CommonUsages.primary2DAxisClick, out bool a) )
    133.         {
    134.             if ( isLeftController )
    135.             {
    136.                 Inputs.leftThumbstickPressed = a;
    137.             }    
    138.  
    139.             else
    140.             {
    141.                 Inputs.rightThumbstickPressed = a;
    142.             }
    143.         }
    144.  
    145.         if ( targetDevice.TryGetFeatureValue(CommonUsages.menuButton, out bool m) )
    146.         {
    147.             if ( isLeftController )
    148.             {
    149.                 Inputs.menuButtonPressed = m;
    150.             }
    151.         }
    152.     }
    153.  
    154.     void GetDevice()
    155.     {
    156.         List<InputDevice> devices = new List<InputDevice>();
    157.  
    158.         InputDevices.GetDevicesWithCharacteristics(controller, devices);
    159.  
    160.         if ( devices.Count > 0 )
    161.         {
    162.             targetDevice = devices[0];
    163.         }
    164.     }
    165. }
    166.  
     
    Last edited: Mar 20, 2023
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    39,571
    Human imagination is not a useful gauge of technical errors.

    The fact that it doesn't work means something DID go wrong.

    As on device and as in editor, same exact process for when something has gone wrong... DEBUGGING!

    Luckily you have it working so you can compare before / after or one scene to the other.

    Time to start debugging! Here is how you can begin your exciting new debugging adventures:

    You must find a way to get the information you need in order to reason about what the problem is.

    Once you understand what the problem is, you may begin to reason about a solution to the problem.

    What is often happening in these cases is one of the following:

    - the code you think is executing is not actually executing at all
    - the code is executing far EARLIER or LATER than you think
    - the code is executing far LESS OFTEN than you think
    - the code is executing far MORE OFTEN than you think
    - the code is executing on another GameObject than you think it is
    - you're getting an error or warning and you haven't noticed it in the console window

    To help gain more insight into your problem, I recommend liberally sprinkling
    Debug.Log()
    statements through your code to display information in realtime.

    Doing this should help you answer these types of questions:

    - is this code even running? which parts are running? how often does it run? what order does it run in?
    - what are the values of the variables involved? Are they initialized? Are the values reasonable?
    - are you meeting ALL the requirements to receive callbacks such as triggers / colliders (review the documentation)

    Knowing this information will help you reason about the behavior you are seeing.

    You can also supply a second argument to Debug.Log() and when you click the message, it will highlight the object in scene, such as
    Debug.Log("Problem!",this);


    If your problem would benefit from in-scene or in-game visualization, Debug.DrawRay() or Debug.DrawLine() can help you visualize things like rays (used in raycasting) or distances.

    You can also call Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene manually, looking for all the parts, where they are, what scripts are on them, etc.

    You can also call GameObject.CreatePrimitive() to emplace debug-marker-ish objects in the scene at runtime.

    You could also just display various important quantities in UI Text elements to watch them change as you play the game.

    If you are running a mobile device you can also view the console output. Google for how on your particular mobile target, such as this answer or iOS: https://forum.unity.com/threads/how-to-capturing-device-logs-on-ios.529920/ or this answer for Android: https://forum.unity.com/threads/how-to-capturing-device-logs-on-android.528680/

    If you are working in VR, it might be useful to make your on onscreen log output, or integrate one from the asset store, so you can see what is happening as you operate your software.

    Another useful approach is to temporarily strip out everything besides what is necessary to prove your issue. This can simplify and isolate compounding effects of other items in your scene or prefab.

    Here's an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong:

    https://forum.unity.com/threads/coroutine-missing-hint-and-error.1103197/#post-7100494

    When in doubt, print it out!(tm)

    Note: the
    print()
    function is an alias for Debug.Log() provided by the MonoBehaviour class.