The question is answered, right answer was accepted
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 ;)
Have fun and thanks
I´d guess (as you mentioned) that it might (!) have to do something with the initialization. Care to post your code for that?
Answer by TR33 · Jan 06, 2018 at 10:55 AM
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 ;)
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[i])
{
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<SpriteRenderer>().color = new Color(1f, 1f, 1f, reducedAlpha);
if (other.GetComponent<Rigidbody2D>().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>().text = SaveManager.Instance.state.goodsOnMapOutput[locationIndex].ToString();
}
private void OnTriggerExit2D(Collider2D other)
{
if (other.tag == "Waggon")
{
other.GetComponent<SpriteRenderer>().color = new Color(1f, 1f, 1f, 1f);
}
MainMenu.Instance.OutputAmount.gameObject.SetActive(false);
}
IEnumerator SpawnCargo(Transform input)
{
isLoading = true;
if (input.GetComponent<ValueSheetWG>().loaded + cargoMass <= input.GetComponent<ValueSheetWG>().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<CargoSheet>().index == cargoInAllowed[j])
{
SaveManager.Instance.AddMoney(waggonInTrigger.transform.Find("Cargo").GetChild(i).GetComponent<CargoSheet>().value);
SaveManager.Instance.DeliverGoods(locationIndex,
waggonInTrigger.transform.Find("Cargo").GetChild(i).GetComponent<CargoSheet>().index,
waggonInTrigger.transform.Find("Cargo").GetChild(i).GetComponent<CargoSheet>().mass);
cargoInput[j]++;
}
}
}
MainMenu.Instance.unload.gameObject.SetActive(false);
}
}
}
If the Array is only ever to have a size of 1 - why use it in the first place? That is what I don´t really get. If every Instance is going to have its own Variable anyway - why not use a simple int? Apart from that - have you tried not initializing via the inspector and just doing it in Code? You could try initializing it at declaration. Ins$$anonymous$$d of public int[] cargoInput; you could try public int[] cargoInput = new int[1]; Or do your arrays have to be different sizes for different instances?
Yes. The arrays will have different sizes on each instance. I tryed initializing but the error still occurres... I think a string might do the job too, but I'm not sure what to do if that also fails :/
Where do you initialize the different sizes, then? $$anonymous$$aybe that is the problem. I couldn´t find any code for that in your script.
Can you try and split the 3rd line like this:
$$anonymous$$ain$$anonymous$$enu.Instance.$$anonymous$$SGPanelUI(cargoInAllowed[0],
cargoInput[0]);
to see which array is causing the error, and can you check the arrays in the inspector after the exception is thrown.
cargoInAllowed is throwing the error. But I'm not sure if that means that cargoInput would ot throw one.
Ok.... I hardcoded a value for cargoInAllowed. cargoInput is still an array. The crazy thing is I don't get the error anymore BUT the values of CargoInput are not the same. In the inspector I can see a 10 in CargoInput but on my Panel (and as a parameter) I am getting 7318343....