Question by
JannickL · Sep 25, 2016 at 07:27 PM ·
instantiateupdatearray-out-of-range-exceptoverflow
Stop instantiate in an update void
Hey there, got a small problem with an inventory system. It will not be an OnGui so don't think bad about the way i'm building it :D
So the problem is that have to call the void from below every update and like you can see the instantiate of "slotImagePrefab" will also be called every frame but there should be just 20 of them. Every time he calls this function he will set int y and int x back to zero an he will endless instantiate the prefab.
How to stop this?
The little distance check a little bit lower need to work and to take a reference he need the iconShow which is changing every update so.
Maybe the solution is easy but my heads capacity is like private int capacity = 0; right now
void Inventory() // Needs to be called every time
{
int i = 0;
for (int y = 0; y < SlotsY; y++) // Slots4 = 5
{
for (int x = 0; x < slotsX; x++) // SlotsX = 5
{
GameObject playerInv = GameObject.Find("Inventory");
GameObject slotImage = Instantiate(slotImagePrefab);
slots[i] = inventory[i];
if (slots[i].itemName != null)
{
string iconName = slots[i].itemName.ToString();
GameObject iconShow = Instantiate(Resources.Load(iconName)) as GameObject;
Vector3 a = iconShow.transform.position; //iconShow has to work
Vector3 b = GameObject.Find("Player").transform.position;
float dist = Vector3.Distance(a, b);
if (dist <= .1f)
{
// Some stuff
}
i++;
}
}
}
}
Thank you guys :)
Comment