Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

154
Views
Assigning gameobject to array but thrown IndexOutOfRangeException

I have a system in place which looks an image, takes every pixel and generates a block to represent each pixel. I am currently in the process of optimising this by creating a combining mesh system so that all the thousands of cube objects are instead a singular mesh.

I have been following this video to do so: https://www.youtube.com/watch?v=NcmPz_nbArY

Essentially, the issue I have is when a cube is generated, I attempt to add it to an array of gameobjects at the position of the array counter (which starts at 0). However, I am always thrown this error:

IndexOutOfRangeException: Index was outside the bounds of the array.
(wrapper stelemref) System.Object.virt_stelemref_sealed_class(intptr,object)

I simply don't understand how. The array starts at position 0, I tell it to set a gameobject at position of the arraycounter which is 0 initially. Even throwing in a Debug.Log(arraycounter) shows that the value is 0, but the error is still thrown.

The point of the array is to act as a parameter which can be thrown into the CombineGroundMeshes() function so the function knows what objects it needs to combine meshes for.

Initially, there would be 2 functions, one for combining ground meshes and one for combining wall meshes but I have resorted to everything going into ground meshes until this issue is resolved. So don't be confused why there are 3 if statements for different coloured pixels going to the same array!

My entire source code can be found at this pastebin. https://pastebin.com/sYjmzsnz

The issue can be located at lines 105 to 144. Where the error occurs is at line 118, 129, and 140.

EDIT: Someone asked for a mini version of my code? I've done my best below to sumarrise it

public GameObject[] groundObjects;
...
foreach(Vector3 pos in spawnpositions)
     counter = 1
     arraycounter = 0
     Color colour = pixel[counter];
     if(colour.equals(Color.White))
     {
          //instantiate gameobject as a transform
          //change transforms localscale
          groundObjects[arraycounter] = spawnedTransform.gameObject;
          // The above line is causing the error (Unity console log)
     }
counter++;                                                                                          
arraycounter++;
...
for (int i = 0; i < groundObjects.Length; i++)
     {
          CombineGroundMeshes(groundObjects[i]);
     }  

over 4 years ago · Santiago Trujillo
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!