Why is this going out of array bounds and why can't I see debug output

using UnityEngine;
using System;
public class GridManager3 : MonoBehaviour

{
    // The GameObject to instantiate
    public float tilesize = 40;
    public float xdispl = -300;
    private float curxd = -300;
    private float curyd = 150;
    public float xspace = 30;
    public float ydispl = 150;
    public float yspace = 30;
    public TI G1;


    private int i = 0;


  
  private int[]xposn=new int[9]{0,1,2,0,1,2,0,1,2};
private int[]yposn=new int[9]{0,0,0,-1,-1,-1,-2,-2,-2};

    //static Sprite[] SA;
    //private int SAN;

    void Start()
    {
        SpawnEntities();
    }

    void SpawnEntities()

    {
        for (i = 0; i < 9; i++)
        {
            Debug.Log(i);
            Debug.Log(xposn[0]);
            Debug.Log(xposn[i]);

            curxd = (xposn[i] * tilesize) + xdispl;
        curyd = (yposn[i] * tilesize) + ydispl;
            {
                for (int j = 1; j < 10; j++)
                {
                    for (int k = 1; k < 10; j++)
                    {
                        Vector3 position;
                        position = new Vector3((xdispl + (i * xspace)), (ydispl - (j * yspace)), 0);

                        TI G1Copy = Instantiate(G1, position, Quaternion.identity);
                        G1Copy.GetComponent<Tileimage>().num = i;
                        G1Copy.GetComponent<TI>().SI(i);
                        G1Copy.GetComponent<TI>().num = i;
                    }
                }
            }

        }
    }
}
using UnityEngine;

public class TI : MonoBehaviour
{
    public int num = 1;
    public Sprite[] sprites;

#pragma warning disable IDE0051 // Remove unused private members
    //void Start()
#pragma warning restore IDE0051 // Remove unused private members
   // {
       // SelectImage();
   // }
    //void SelectImage()
   // {
       // this.gameObject.GetComponent<SpriteRenderer>().sprite = sprites[num - 1];
  //  }
    public void SI(int mynum)
    {
        this.gameObject.GetComponent<SpriteRenderer>().sprite = sprites[mynum];
    }
}

}
}
}

}
}
}

Here are some notes on IndexOutOfRangeException and ArgumentOutOfRangeException:

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

Steps to success:

  • find which collection it is (critical first step!)
  • find out why it has fewer items than you expect
  • fix whatever logic is making the indexing value exceed the collection
  • remember you might have more than one instance of this script in your scene/prefab

Make sure your log console selector buttons are enabled. See this graphic:

https://discussions.unity.com/t/733002/10

https://discussions.unity.com/t/804947/4

1 Like

Copy and paste your error message. But my first guess is it’s your sprites array.

2 Likes

Sprites array but its out of bound in element 0 apparent

As in the array is empty. There is no element, not even one marked zero.

It has 9 elements. I have having lots of trouble with running Unity so can’t get the output.

Unity is locked up then crash and using 87% of memory some 2000 MB. I accidentally loaded the experimental version. but I had the same problem with the 2020 release version

7593388–941776–complete sprite array.pdf (202 KB)

If Unity crashes, you usually have an infinite loop. Check your loops. The middle and last one specifically. For each j, you iterate 10 k’s, but each time increment j again. Or in other words, each j adds 10 new j’s. So j is approaching infinity. The loop runs until something on your computer cannot handle it anymore.

1 Like

for (int k = 1; k < 10; j++) , Should be k++. You are never incrementing k in your loop, so it is infinite!

3 Likes

Great catch!

1 Like

When you wrote literally the same thing earlier and someone else reposts it and gets all the recognition haha.
Reminds me of school :roll_eyes:

Good point, less words caught my attention “more”

Happens, i guess his answer was more clear.
To be fair, i argued that j would approach infinity as well, while technically the k part of the loop would already repeat indefinitely. So there is that.

Anyways, i was just thinking it’s funny haha. Either way i hope OPs problem is solved.