Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
This question was closed Jan 06, 2018 at 03:00 PM by TR33 for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by TR33 · Jan 05, 2018 at 11:25 PM · error messagearraysindexoutofrangeexception

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

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image TobiKatze · Jan 05, 2018 at 11:39 PM 0
Share

I´d guess (as you mentioned) that it might (!) have to do something with the initialization. Care to post your code for that?

1 Reply

  • Sort: 
avatar image
0

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);
         }        
     }
 
 }
 

Comment
Add comment · Show 19 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image TobiKatze · Jan 06, 2018 at 11:38 AM 0
Share

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?

avatar image TR33 TobiKatze · Jan 06, 2018 at 11:48 AM 0
Share

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 :/

avatar image TobiKatze TR33 · Jan 06, 2018 at 11:50 AM 0
Share

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.

Show more comments
avatar image YoucefB · Jan 06, 2018 at 12:16 PM 0
Share

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.

avatar image TR33 YoucefB · Jan 06, 2018 at 12:28 PM 0
Share

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....

avatar image YoucefB TR33 · Jan 06, 2018 at 12:42 PM 0
Share

can you take a screenShot of that

Show more comments

Follow this Question

Answers Answers and Comments

123 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Array of objects works half the time! 0 Answers

Array Index is out of range 0 Answers

IndexOutOfRangeException: Array index is out of range. on "frequency = frequencies[thisFreq];" 0 Answers

C# ArrayList Accessing and RemoveAt? 0 Answers

NullReferenceException randomly occurs 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges