Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

Bug script saying Index was out of range. Must be non-negative and less than the size of collection

Discussion in 'Editor & General Support' started by ca_ka_studio, May 7, 2024.

  1. ca_ka_studio

    ca_ka_studio

    Joined:
    Apr 24, 2024
    Posts:
    2
    Hey so for my game this script is for finger movement, for some reason its giving this error : blob:https://photos.onedrive.com/e7947052-0402-48ce-b44b-7fc58bff44f0
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.XR;
    5. using Photon.Pun;
    6. using Photon.Realtime;
    7.  
    8. public enum HandTypeLeft
    9. {
    10.     Left
    11. }
    12.  
    13. public class XRHandL : MonoBehaviour
    14. {
    15.     public HandTypeLeft handType;
    16.     public float thumbMoveSpeed = 0.1f;
    17.  
    18.     private Animator animator;
    19.     private InputDevice inputDevice;
    20.  
    21.     private float pose4Value;
    22.     private float pose5Value;
    23.     private float pose6Value;
    24.     public PhotonView view;
    25.  
    26.     // Start is called before the first frame update
    27.     void Start()
    28.     {
    29.         animator = GetComponent<Animator>();
    30.         inputDevice = GetInputDevice();
    31.     }
    32.  
    33.     // Update is called once per frame
    34.     void Update()
    35.     {
    36.         if (view.IsMine)
    37.         {
    38.             AnimateHand();
    39.         }
    40.     }
    41.  
    42.     InputDevice GetInputDevice()
    43.     {
    44.         InputDeviceCharacteristics controllerCharacteristic = InputDeviceCharacteristics.HeldInHand | InputDeviceCharacteristics.Controller;
    45.  
    46.         if (handType == HandTypeLeft.Left)
    47.         {
    48.             controllerCharacteristic = controllerCharacteristic | InputDeviceCharacteristics.Left;
    49.         }
    50.         else
    51.         {
    52.             controllerCharacteristic = controllerCharacteristic | InputDeviceCharacteristics.Right;
    53.         }
    54.  
    55.         List<InputDevice> inputDevices = new List<InputDevice>();
    56.         InputDevices.GetDevicesWithCharacteristics(controllerCharacteristic, inputDevices);
    57.  
    58.         return inputDevices[0];
    59.     }
    60.  
    61.     void AnimateHand()
    62.     {
    63.         inputDevice.TryGetFeatureValue(CommonUsages.trigger, out pose4Value);
    64.         inputDevice.TryGetFeatureValue(CommonUsages.grip, out pose5Value);
    65.  
    66.         inputDevice.TryGetFeatureValue(CommonUsages.primaryTouch, out bool primaryTouched);
    67.         inputDevice.TryGetFeatureValue(CommonUsages.secondaryTouch, out bool secondaryTouched);
    68.  
    69.         if (primaryTouched || secondaryTouched)
    70.         {
    71.             pose6Value += thumbMoveSpeed;
    72.         }
    73.         else
    74.         {
    75.             pose6Value -= thumbMoveSpeed;
    76.         }
    77.  
    78.         pose6Value = Mathf.Clamp(pose6Value, 0, 1);
    79.  
    80.         animator.SetFloat("pose4", pose4Value);
    81.         animator.SetFloat("pose5", pose5Value);
    82.         animator.SetFloat("pose6", pose6Value);
    83.     }
    84. }
     
  2. ca_ka_studio

    ca_ka_studio

    Joined:
    Apr 24, 2024
    Posts:
    2
  3. ArachnidAnimal

    ArachnidAnimal

    Joined:
    Mar 3, 2015
    Posts:
    1,936
    GetDevicesWithCharacteristics found no devices.
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    39,335
    Here are some notes on IndexOutOfRangeException and ArgumentOutOfRangeException:

    http://plbm.com/?p=236

    Steps to success:

    - find which collection it is and what line of code accesses it <--- (critical first step!)
    - find out why it has fewer items than you expect
    - fix whatever logic is making the indexing value exceed the collection size

    Remember also:

    - a collection with ZERO elements cannot be indexed at all: it is empty (as Arachnid notes above!)
    - you might have more than one instance of this script in your scene/prefab
    - the collection may be used in more than one location in the code
    - indices start at ZERO (0) and go to the count / length minus 1.

    This means with three (3) elements in your collection, they are numbered 0, 1, and 2 only.