Array Index Range Problems

Hello :slight_smile:

I have a function that checks if there is player save data for my game. If there is no data found it creates a new file. The file uses a PlayerData class that contains various data. Three of the variables are arrays that I use to hold Extension information:

class PlayerData
{
  // ...other data blah blah blah
  public PlayerExtensionController.ExtensionType[] CurrentBaseExtensionSlots = new PlayerExtensionController.ExtensionType[4];

  public PlayerExtensionController.ExtensionType[] CurrentMidExtensionSlots = new PlayerExtensionController.ExtensionType[6];

  public PlayerExtensionController.ExtensionType[] CurrentMaxExtensionSlots = new PlayerExtensionController.ExtensionType[8];
}

In my Load() method, I use a binary formater to grab the info and put it into a PlayerData object, then I pass that info to the class that handles player data in game.

if (File.Exists(Application.persistentDataPath + "/playerSave.dat"))
  {
  Debug.Log("Player Save Data Found! Loading from " + Application.persistentDataPath + "/playerSave.dat");
  BinaryFormatter bf = new BinaryFormatter();
  FileStream file = File.Open(Application.persistentDataPath + "/playerSave.dat", FileMode.Open);
  PlayerData data = (PlayerData)bf.Deserialize(file);
  file.Close();
  
// other data blah blah blah
data.CurrentBaseExtensionSlots.CopyTo(BaseExtensionSlots, 0);
  data.CurrentMidExtensionSlots.CopyTo(MidExtensionSlots, 0);
  data.CurrentMaxExtensionSlots.CopyTo(MaxExtensionSlots, 0);
}

I get an out of range exception for this, am I using this wrong or am I supposed to initialize the arrays somewhere? I have the Base/Mid/MaxExtensionSlots initialized in Start() to their proper sizes. Any help is appreciated.

Is BaseExtensionSlots/MidExtensionSlots/MaxExtensionSlots declared somewhere with a new and has the correct array size?

Yes, in Start().

void Start()
  {
  PlayerExtensionController.ExtensionType[] BaseExtensionSlots = new PlayerExtensionController.ExtensionType[4];
   
  PlayerExtensionController.ExtensionType[] MidExtensionSlots = new PlayerExtensionController.ExtensionType[6];
   
  PlayerExtensionController.ExtensionType[] MaxExtensionSlots = new PlayerExtensionController.ExtensionType[8];

  }

What is the actual error message?

ArgumentException: Destination array was not long enough. Check destIndex and length, and the array’s lower bounds.
System.Array.CopyTo (System.Array array, Int32 index) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System/Array.cs:1830)
PlayerInfoHandler.Load () (at Assets/_Scripts/PlayerInfoHandler.cs:611)
SceneControl.OnLevelWasLoaded () (at Assets/_Scripts/SceneControl.cs:90)

I’m assuming SceneControl.cs:90 is one of these 3 lines:

data.CurrentBaseExtensionSlots.CopyTo(BaseExtensionSlots, 0);
  data.CurrentMidExtensionSlots.CopyTo(MidExtensionSlots, 0);
  data.CurrentMaxExtensionSlots.CopyTo(MaxExtensionSlots, 0);
}

probably the first one?
You can always add a debug before them alike this to check that they are the right size

Debug.Log("CurrentBase Length is: " + data.CurrentBaseExtensionSlots.Length  + ", BaseExtensionSLots length is: " + BaseExtensionSlots.Length);

I’m not familiar with ExtensionType. Is that some MSDN or Unity class… or is it a class you’ve made. Either way if you do a line like this:

 PlayerExtensionController.ExtensionType[] BaseExtensionSlots = new PlayerExtensionController.ExtensionType[4];

And Extension Type is a class all you’ve done is set aside memory to hold the addresses of where 4 ExtensionType classes will be. You haven’t actually allocated space for them. you’d also need to do this:

for (int i=0;i<4;++1)
      BaseExtensionSlot[i] = new PlayerExtensionController.ExtensionType();
1 Like

I was away for the past couple of days so I didn’t have time to respond. I tried this and I’m still getting a “Array Index is out of Range”.

ExtensionType is an enum I created to know what extensions the player is using. Apparently I can’t use enum like this in C# so I’ll just try something else unless someone else as an idea.

You should be able to. You won’t need to do the new thing I did since enums aren’t classes… they are like primitives.
Did you try debugging the Lengths of both the source and dest arrays before you do the CopyTo?

1 Like

I did some debugging and found out that indeed, that BaseExtensionSlots[ ] (and the rest) were not the correct length. I placed this code into Start() but I’m not really sure why it wasn’t working.

Thanks for the help takatok!