• 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 Feb 21, 2018 at 08:39 AM by meat5000 for the following reason:

Common question - Check your if statements and remember that counting starts from 0

avatar image
0
Question by Rosh7 · Feb 21, 2018 at 02:45 AM · error messagearrays

Array Index is out of range

Hello, I am currently writing my crafting script that uses UI (as you will probably see in the script). I have my for loop in AssignRecipes that is called from my main inventory script. This method takes all of the recipes that the player has learned and assigns them to buttons in the UI. X is for the recipe array and Z is for the buttons array. recipes[x] works fine throughout the method until: buttons[z].onClick.AddListener(delegate { AssignDescriptions(recipes[x]); }); When it gets here I get the IndexOutOfRangeException: Array index is out of range error. I have no idea why just here and no where else. When x in this line is replaced by 0 it works fine btw, and no, it's not a problem with the inspector array length. Crafting_Recipes is a scripts bleeding object. Script:

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

public class Crafting_002 : MonoBehaviour {

 [Header("Scripts")]
 public Inventory_001 inventory;
 public Equipment_001 equipment;

 [Header("Recipes")]
 public Crafting_Recipes[] recipes = new Crafting_Recipes[10];

 [Header("Buttons")]
 public Button[] buttons = new Button[10];

 [Header("Texts")]
 public Text[] buttonTexts = new Text[10];
 public Text itemDesc;
 public Text itemRecipe;



 public void AssignRecipes ()
 {
     int z = 0;
     for (int x = 0; x < recipes.Length; x++)
     {
         //print(x);
         print(recipes.Length);
         if (recipes[x] != null && recipes[x].learned == true)
         {
             print(recipes[x].name);
             buttons[z].gameObject.SetActive(true);
             buttonTexts[z].text = recipes[x].name;
             print(x);
             if (x == 0)
             {
                 buttons[z].onClick.AddListener(delegate { AssignDescriptions(recipes[x]); });
             }
             z++;
         }
     }
 }

 public void AssignDescriptions (Crafting_Recipes recipe)
 {
     itemDesc.gameObject.SetActive(true);
     itemDesc.text = recipe.description;
     itemRecipe.text = recipe.recipe;
 }

}

Comment
Add comment · Show 2
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 RobAnthem · Feb 21, 2018 at 08:18 AM 0
Share

This would be a good time to add some Debug.Log() output to see what is going on. $$anonymous$$y assumption is that the Length of buttons is somehow 0. Even though your code says buttons is a length of 10, I can't know further without seeing your inspector window.

avatar image Rosh7 · Feb 21, 2018 at 11:37 AM 0
Share

I can send my inspector window tonight when I get home. I don't know why this question was closed as I have stated I know where the problem is. I know counting starts from zero, my if statements are fine, and my arrays are the right length because they are public and I have things in the arrays in the inspector. It is inside the delegate where I have the problem, if I replace the X in there with a 0 it works fine. I have many print statements in there that tell me lengths of arrays and whay x is.

0 Replies

  • Sort: 

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

80 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

Related Questions

C# ArrayList Accessing and RemoveAt? 0 Answers

NullReferenceException randomly occurs 2 Answers

IndexOutOfRangeException: Index was outside the bounds of the array at com.unity.inputsystem@1.2.0/InputSystem/Editor/AssetEditor/InputActionEditorToolbar.cs 0 Answers

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

Error In The Script 1 Answer


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