Does Anyone Know Why My Cube Script Is Creating an Error?

So, I have decided to make a 3D Software in unity similar to Blender using OpenXR, I have recently tried adding vertex movement through the XR simple grab by Instantiating objects at vertices with the grab script. However, after adding this I have received the error

``IndexOutOfRangeException: Index was outside the bounds of the array.
ProceduralCube.faceVertices (System.Int32 dir, System.Single scale, UnityEngine.Vector3 facePos) (at Assets/ProceduralCube.cs:38)
ProceduralCube.MakeFace (System.Int32 dir, System.Single faceScale, UnityEngine.Vector3 facePos) (at Assets/ProceduralCube.cs:90)
ProceduralCube.MakeCube (System.Single cubeScale, UnityEngine.Vector3 cubePos) (at Assets/ProceduralCube.cs:85)
ProceduralCube.Update () (at Assets/ProceduralCube.cs:66)`

The Script is here if anyone can help

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[RequireComponent( typeof(MeshFilter), typeof(MeshRenderer) )]

public class ProceduralCube : MonoBehaviour
{
    public GameObject VertexBaseRenderedOBJ;
    List<GameObject> Sphere = new List<GameObject>();

    public Vector3[] Vertices = 
    {
        new Vector3( 1, 1, 1),
        new Vector3(-1, 1, 1),
        new Vector3(-1,-1, 1),
        new Vector3( 1,-1, 1),
        new Vector3(-1, 1,-1),
        new Vector3( 1, 1,-1),
        new Vector3( 1,-1,-1),
        new Vector3(-1,-1,-1)
    };
    

    public int[][] FaceTriangles = {
        new int[] {0, 1, 2, 3},
        new int[] {5, 0, 3, 6},
        new int[] {4, 5, 6, 7},
        new int[] {1, 4, 7, 2},
        new int[] {5, 4, 1, 0},
        new int[] {3, 2, 7, 6}
    };

    public Vector3[] faceVertices(int dir, float scale, Vector3 facePos) {
        Vector3[] fv = new Vector3[4];
        for (int i = 0; i < fv.Length; i++)
        {
            fv[i] = Vertices[FaceTriangles[dir] [i]] * scale + facePos;
        }
        return fv;

    }
    Mesh mesh;
    public float scale = 1f;
    public int posX,posY,posZ;
    float adjScale;
    List<Vector3> vertices;
    List<int> triangles;
    private void Awake() 
    {
        
        mesh = GetComponent<MeshFilter>().mesh;
        adjScale = scale * 0.5f;
    }

    private void Start() 
    {

        
    }
    private void Update() 
    {
        
        RenderAllSpheres();
        UpdateVertices();
        MakeCube(adjScale, new Vector3((float) posX, (float) posY, (float) posZ) * scale);
        UpdateMesh();
    }

    void UpdateMesh() 
    {
        mesh.Clear();

        mesh.vertices = vertices.ToArray();
        mesh.triangles = triangles.ToArray();
        mesh.RecalculateNormals();
    }
    void MakeCube(float cubeScale, Vector3 cubePos) 
    {
        vertices = new List<Vector3>();
        triangles = new List<int>();

        for (int i = 0; i < 6; i++)
        {
            MakeFace (i, cubeScale, cubePos);
        }
    }
    void MakeFace(int dir, float faceScale, Vector3 facePos) 
    {
        vertices.AddRange(faceVertices(dir, faceScale, facePos));
        int vCount = vertices.Count;

        triangles.Add(vCount - 4);
        triangles.Add(vCount - 4 + 1);
        triangles.Add(vCount - 4 + 2);
        triangles.Add(vCount - 4);
        triangles.Add(vCount - 4 + 2);
        triangles.Add(vCount - 4 + 3);
    }
    void UpdateVertices() {
        for (int i = 0; i < Vertices.Length; i++)
        {
            if(Vertices[i] == Sphere[i].transform.position) continue;
        }
    }
    void RenderAllSpheres() {

    while (Sphere.Count < Vertices.Length)
    {

        Sphere.Add(null);
    }
    
    for (int i = 0; i < Vertices.Length; i++)
    {

        if (Sphere[i] == null)
        {
            Sphere[i] = Instantiate(VertexBaseRenderedOBJ, Vertices[i], Quaternion.identity);
        }
    }
}

}

Surely if you’re making a 3d software you can debug a simple index out of range exception.

The error is on line 38. You’re accessing 3 different arrays all in one line so break down the code and work out in which one is being accessed out of range.

Yeah, as Spiney says, always the same answer for this trivial bug.

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
  • 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.