Error: IndexOutOfRange: call for index 0 in an array with length 1(+)

Hi guys,

I’m getting a super confusing exception while I’m working with arrays. The size of the arrays is set in the inspector / Component of a Prefab and then used in the script. The prefab will be initialized mutiple times. Maybe that’s where the error is coming from, but I’m super confused right now, because it does what it is supposed to do AND throwing an error…

 if (SaveManager.Instance.missionFinished)
        {
            MainMenu.Instance.MSGPanelUI(cargoInAllowed[0], cargoInput[0]);
            cargoInput[0] = 0;
            SaveManager.Instance.missionFinished = false;
        }

More specific information:

→ cargoInAllowed has a size of 1 and at position 0 is a 0,
→ cargoInput has the same size as cargoInAllowed and at position 0 the script is writing a number. So it basically works as some kind of temporary memory.

However. The error occurres in line 3 (MainMenu.Instance…)

    //MSGPanel GameUI
    public void MSGPanelUI(int cargoIndex, int amount)
    {
        MSGGameUI.SetActive(true);
        RectTransform CG = Instantiate(SaveManager.Instance.Cargo_Prefabs[cargoIndex]);
        CG.SetParent(MSGGameUI.transform.GetChild(0));
        CG.transform.GetChild(1).GetComponent<Text>().text = amount.ToString();
    }

this is the function I’m calling. Cargo_Prefabs has the length of 1 too and a RectTransfrom assigned to it. I don’t think I’m doing something special here so if someone knows why this error occurres feel free to say it :wink:

Have fun and thanks

TobiKatze

So this is the script attached to the prefab. I thought every instance of the object would have it’s own array. Currently I’m thinking about a workaround using strings.
BTW: I know it’s messy but I was just going nuts because it didn’t work out as I thought it would :wink:

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

public class LocationScript : MonoBehaviour {

    public GameObject cargoOutput;
    public Transform cargoSpawnPoint;
    public float reducedAlpha = .5f;
    public int[] cargoInAllowed;
    public int[] cargoInput;
    public int locationIndex;

    private bool isLoading = false;
    private int cargoMass;
    private GameObject waggonInTrigger;

    private void Start()
    {
        cargoMass = cargoOutput.GetComponent<CargoSheet>().mass;
    }

    private void Update()
    {
        if (SaveManager.Instance.missionFinished)
        {
            MainMenu.Instance.MSGPanelUI(cargoInAllowed[0], cargoInput[0]);
            cargoInput[0] = 0;
            SaveManager.Instance.missionFinished = false;
        }
    }

    private void OnTriggerEnter2D(Collider2D other)
    {
        if (other.tag == "Cargo")
        {
            for (int i = 0; i < cargoInAllowed.Length; i++)
            {
                if (other.GetComponent<CargoSheet>().index == cargoInAllowed*)*

{
MainMenu.Instance.unload.gameObject.SetActive(true);
}
}
}
MainMenu.Instance.OutputAmount.gameObject.SetActive(true);
}

private void OnTriggerStay2D(Collider2D other)
{
if (other.tag == “Waggon”)
{
waggonInTrigger = other.gameObject;
other.GetComponent().color = new Color(1f, 1f, 1f, reducedAlpha);

if (other.GetComponent().velocity.magnitude <= .1f)
{
if (!isLoading)
{
StartCoroutine(SpawnCargo(other.transform));
}
}
}
else if (other.tag == “Cargo” && SaveManager.Instance.isUnloading == true)
{
UnloadCargo();
SaveManager.Instance.isUnloading = false;
}
MainMenu.Instance.OutputAmount.GetComponentInChildren().text = SaveManager.Instance.state.goodsOnMapOutput[locationIndex].ToString();
}

private void OnTriggerExit2D(Collider2D other)
{
if (other.tag == “Waggon”)
{
other.GetComponent().color = new Color(1f, 1f, 1f, 1f);
}
MainMenu.Instance.OutputAmount.gameObject.SetActive(false);
}

IEnumerator SpawnCargo(Transform input)
{
isLoading = true;
if (input.GetComponent().loaded + cargoMass <= input.GetComponent().loadingCapa
&& SaveManager.Instance.state.goodsOnMapOutput[locationIndex] >= cargoMass)
{
Instantiate(cargoOutput, cargoSpawnPoint.position, Quaternion.identity);
SaveManager.Instance.state.goodsOnMapOutput[locationIndex] -= cargoMass;
yield return new WaitForSeconds(2);
}
else
{
yield return new WaitForSeconds(1);
}

isLoading = false;
}

public void UnloadCargo()
{
if (waggonInTrigger == null)
{
return;
}
else
{
int unloadTemp = waggonInTrigger.transform.Find(“Cargo”).childCount;
for (int i = 0; i < unloadTemp; i++)
{
waggonInTrigger.transform.Find(“Cargo”).GetChild(i).gameObject.layer = 12;
for (int j = 0; j < cargoInAllowed.Length; j++)
{
if (waggonInTrigger.transform.Find(“Cargo”).GetChild(i).GetComponent().index == cargoInAllowed[j])
{
SaveManager.Instance.AddMoney(waggonInTrigger.transform.Find(“Cargo”).GetChild(i).GetComponent().value);
SaveManager.Instance.DeliverGoods(locationIndex,
waggonInTrigger.transform.Find(“Cargo”).GetChild(i).GetComponent().index,
waggonInTrigger.transform.Find(“Cargo”).GetChild(i).GetComponent().mass);
cargoInput[j]++;
}
}
}
MainMenu.Instance.unload.gameObject.SetActive(false);
}
}

}