Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

ARRAY INDEX IS OUT RANGE?!??!

Discussion in 'Scripting' started by zayan24, Jul 21, 2016.

  1. zayan24

    zayan24

    Joined:
    Jan 27, 2015
    Posts:
    8
    I am following BurgZergArcade's Hack and Slash tut with Unity 2.6.1. I downloaded it last week to follow along correctly. Now I'm on the 30th tutorial: Saving Character Data 4/6. I tested it at one point and when I change the Constitution to ANYTHING, it won't let me create the character and gives me an error: IndexOutOfRangeException: Array index is out of range. Here's my script and the error is at line 36.

    using UnityEngine;
    using System.Collections;
    using System;

    public class GameSettings : MonoBehaviour {

    void Awake() {
    DontDestroyOnLoad(this);
    }

    // Use this for initialization
    void Start () {

    }

    // Update is called once per frame
    void Update () {

    }

    public void SaveCharacterData() {
    GameObject pc = GameObject.Find("pc");

    PlayerCharacter pcClass = pc.GetComponent<PlayerCharacter>();

    // PlayerPrefs.DeleteAll();

    PlayerPrefs.SetString("Player Name", pcClass.Name);

    for(int cnt = 0; cnt < Enum.GetValues(typeof(AttributeName)).Length; cnt++) {
    PlayerPrefs.SetInt(((AttributeName)cnt).ToString() + " - Base Value", pcClass.GetPrimaryAttribute(cnt).BaseValue);
    PlayerPrefs.SetInt(((AttributeName)cnt).ToString() + " - Exp To Level", pcClass.GetPrimaryAttribute(cnt).ExpToLevel);
    }

    for(int cnt = 0; cnt < Enum.GetValues(typeof(AttributeName)).Length; cnt++) {
    PlayerPrefs.SetInt(((AttributeName)cnt).ToString() + " - Base Value", pcClass.GetVital(cnt).BaseValue);
    PlayerPrefs.SetInt(((VitalName)cnt).ToString() + " - Exp To Level", pcClass.GetVital(cnt).ExpToLevel);
    PlayerPrefs.SetInt(((VitalName)cnt).ToString() + " - Exp To Cur Value", pcClass.GetVital(cnt).CurValue);
    }

    }

    public void LoadCharacterData() {
    }
    }
     
  2. gorbit99

    gorbit99

    Joined:
    Jul 14, 2015
    Posts:
    1,350
    Ryiah likes this.
  3. tonemcbride

    tonemcbride

    Joined:
    Sep 7, 2010
    Posts:
    1,077
    It looks like the second loop should be using 'VitalName' rather than 'AttributeName' - it's using the counter for 'AttributeName' and referencing VitalName.

    I did a quick google search and found the same code but with corrections to the second part:

    Code (CSharp):
    1. for(int cnt = 0; cnt &lt; Enum.GetValues(typeof(VitalName)).Length; cnt++) {
    2.          PlayerPrefs.SetInt(((VitalName)cnt).ToString() + " - Base Value", pcClass.GetVital(cnt).BaseValue);
    3.          PlayerPrefs.SetInt(((VitalName)cnt).ToString() + " - Exp To Level", pcClass.GetVital(cnt).ExpToLevel);
    4.          PlayerPrefs.SetInt(((VitalName)cnt).ToString() + " - Current Value", pcClass.GetVital(cnt).CurValue);
     
  4. novashot

    novashot

    Joined:
    Dec 12, 2009
    Posts:
    373
    An array with 10 items returns a length of 10.... but arrays start at index 0. In this case array[0] through array [9], your loops go 0 through 10 and 10 is out of index range.

    Looping arrays use array.Length - 1
     
  5. gorbit99

    gorbit99

    Joined:
    Jul 14, 2015
    Posts:
    1,350
    He used a < symbol, so if length is 10, he'll go from 0 to 9
     
  6. novashot

    novashot

    Joined:
    Dec 12, 2009
    Posts:
    373
    Ha true enough. It shows I only skimmed the message...
     
  7. BluHornet

    BluHornet

    Joined:
    Feb 23, 2015
    Posts:
    40
    try storing "Enum.GetValues(typeof(AttributeName)).Length" as an int. I have had trouble calling methods with methods like you have posted here.
     
  8. zayan24

    zayan24

    Joined:
    Jan 27, 2015
    Posts:
    8
    I noticed that and fixed it but it didn't help
     
  9. zayan24

    zayan24

    Joined:
    Jan 27, 2015
    Posts:
    8
    I believe they are ints.
     
  10. zayan24

    zayan24

    Joined:
    Jan 27, 2015
    Posts:
    8
    Could someone just give me a solid answer?
     
  11. AndrewTheLeet

    AndrewTheLeet

    Joined:
    Jul 16, 2016
    Posts:
    6
    Regardless of this error you shouldn't be saving that kind of data in the PlayerPrefs anyway. PlayerPrefs should be used to store the users settings such as their preferred object detail level, preferred resolution etc. To store data relating to the game such as the players experience you should use XmlSerialization.

    Hope this artical helps :) - http://wiki.unity3d.com/index.php?title=Saving_and_Loading_Data:_XmlSerializer
     
  12. zayan24

    zayan24

    Joined:
    Jan 27, 2015
    Posts:
    8
    I'm just doing what the BurgZerg did. Hey, I changed the < to a >. It works now but will that affect my game>
     
  13. AndrewTheLeet

    AndrewTheLeet

    Joined:
    Jul 16, 2016
    Posts:
    6
    Well if your planning on your game being played in a web player then PlayerPrefs will be capped at 1MB so keep that in mind other then that if your not saving large quantity's of data the performance hit will be negligible however if you are saving large quantity's of data you should switch to using XmlSerialization because it is faster.
     
  14. zayan24

    zayan24

    Joined:
    Jan 27, 2015
    Posts:
    8
    That's not what I asked but thanks.........I guess
     
  15. AndrewTheLeet

    AndrewTheLeet

    Joined:
    Jul 16, 2016
    Posts:
    6
    Oh derp, I just realized what you meant :eek:.
    "<" is more than ">" is less than.
    So no this will not negatively affect your game it was just a typo by the tutorials author.

    For example:
    if(1 < 2) { } // if 1 is smaller then 2.
    if(1 > 2) { } // if 1 is bigger then 2.
     
    Last edited: Jul 22, 2016
  16. zayan24

    zayan24

    Joined:
    Jan 27, 2015
    Posts:
    8
    huh?
     
  17. zayan24

    zayan24

    Joined:
    Jan 27, 2015
    Posts:
    8
    Could you rephrase it though?
     
  18. Mycroft

    Mycroft

    Joined:
    Aug 29, 2012
    Posts:
    160
    You need to learn to write code.


    If you're changing < to >, stop getting warnings and don't know how this will affect the rest of the project then you're not really learning.

    Take a step back and do some starting C# tutorials.
     
  19. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
     
    Mycroft likes this.