Search Unity

Resolved index out of range exeption

Discussion in 'VR' started by teatime_penguin, Sep 26, 2022.

  1. teatime_penguin

    teatime_penguin

    Joined:
    May 3, 2022
    Posts:
    25
    does anyone know what this means
    I get this in my code on line 34-36 and then it freezes up everything, but it works perfectly fine with similar code a few lines down on line 42-44 and i can't find anything on google about this. if anyone could help it would be appreciated. here is my code.


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.XR;
    5.  
    6.  
    7. public class gunmagizen : MonoBehaviour
    8. {
    9.     public GameObject [] bullets;
    10.     public int bnumber;
    11.     public float bnumberfloat;
    12.     public bool minus = false;
    13.     public hand hand;
    14.     public bool pluse = false;
    15.     public GameObject bottem;
    16.     bool minusdone = false;
    17.     bool plusdone = false;
    18.     // Start is called before the first frame update
    19.     void Start()
    20.     {
    21.         bnumber = bullets.Length;
    22.     }
    23.  
    24.     // Update is called once per frame
    25.     void Update()
    26.     {
    27.         if (bnumber > bullets.Length)
    28.         {
    29.          bnumber = bullets.Length;
    30.          }
    31.         bnumberfloat = bnumber;
    32.         if (minus && bnumberfloat - 1 > -1 && minusdone == false)
    33.         {
    34.             Debug.Log(bnumber);
    35.             bullets[bnumber].GetComponent<Collider>().enabled = false;
    36.             bullets[bnumber].GetComponent<MeshRenderer>().enabled = false;
    37.             bottem.transform.position = bullets[bnumber].transform.position;
    38.             bnumber--;
    39.             minusdone = true;
    40.         }
    41.         if (pluse && bnumber < bullets.Length && plusdone == false)
    42.         {
    43.             bullets[bnumber].GetComponent<Collider>().enabled = true;
    44.             bullets[bnumber].GetComponent<MeshRenderer>().enabled = true;
    45.             bottem.transform.position = bullets[bnumber].transform.position;
    46.             bnumber++;
    47.             plusdone = true;
    48.         }
    49.         if (hand.leftgrip)
    50.         {
    51.             minus = true;
    52.         }
    53.         else
    54.         {
    55.             minus = false;
    56.             minusdone = false;
    57.         }
    58.         if (hand.rightgrip)
    59.         {
    60.             pluse = true;
    61.         }
    62.         else
    63.         {
    64.             pluse = false;
    65.             plusdone = false;
    66.         }
    67.     }
    68.  
    69. }
    70.  
     
    Last edited: Sep 28, 2022
  2. glenneroo

    glenneroo

    Joined:
    Oct 27, 2016
    Posts:
    231
    1. Please always put code inside CODE blocks otherwise it's unreadable.
    2. Is this an AR/VR specific problem? If not it doesn't belong here. I would suggest: https://forum.unity.com/forums/scripting.12/
    3. You will need to at least show the console output where it's giving an error. If you click on the error in the Console it will show you a stack trace with more information, specifically, who called the method and on which line it crashed. Don't do this here though.
    4. Once you have found which line the error is occurring, consider using your debugger (in VSCode or Visual Studio Community or Rider) to step through your code to look at the values.