0

So I've tried to create a randomization function for a game to randomly generate characters from an array and add the result into another however it hasn't really worked and keeps throwing an "Index was outside the bounds of the array" error and I'm not too sure why. :( I've looked online and haven't really found any solutions that worked.

I've tried changing the dice to start at zero or one as well as changing the maximum number accordingly, I've also changed the character array from dynamic to fixed and nothing I've done has helped as its still the same line with the same error

Screenshot of code throwing the error

the array was declared as (couldn't fit it in a screenshot)

string[] player1_characters = new string[13] { "Xingqiu", "Razor", "Bennett", "Aether", "Kaeya", "amber", "fischl", "noelle", "Lisa", "Xinyan", "Ningguang", "chongyun", "barbara" };

The other array is declared as:

 string[] player1_output = new string[] { };

I'm pretty new to c# and don't know what's going on with it XD any help would be amazing ^^

Trevor
  • 7,777
  • 6
  • 31
  • 50
xillvaria
  • 1
  • 1
  • 5
    Please don't post screenshots of the code. Post the actual code as text. Also, if you can, try to create a [mcve]. – Lasse V. Karlsen Mar 05 '21 at 15:00
  • 1
    How big are `player1_characters`, and how big is `player1_output` ? Also, your question shows us what `player2_characters` look like, but the code in your screenshot is not using that one. Could it be a simple mistake of using the wrong array? – Lasse V. Karlsen Mar 05 '21 at 15:00
  • Either `player1_output` doesn't have any elements, or `player1_charcters` doesn't have an index of what `dice` was. Please post a [mre]. – gunr2171 Mar 05 '21 at 15:01
  • 1
    no worries! ill edit that now :) – xillvaria Mar 05 '21 at 15:02
  • 1
    Hello and welcome to SO! Please read the [how-to-ask page](https://stackoverflow.com/help/how-to-ask) and read [How to create a Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/minimal-reproducible-example) to improve your question and help us to understand your problem. – Trevor Mar 05 '21 at 15:02
  • If you have the code `string[] player1_output = new string[] { };`, then the array doesn't have an index 0, hence the error. See the link above on why. – gunr2171 Mar 05 '21 at 15:08

0 Answers0