2021年1月29日星期五

why isn't this code working? Error - Index was out of range Must be non-negative and less than the size of the collection

I am trying to make it for my script to randomly choose a GameObject, but I am getting this error, ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index. I have also changed the count value to 0, but still nothing, please help?

    public GameObject OptionA;      public GameObject OptionB;      public GameObject OptionC;      public GameObject OptionD;        void Start()      {          optionsList.Add(OptionA);          optionsList.Add(OptionB);          optionsList.Add(OptionC);          optionsList.Add(OptionD);        }          List<GameObject> optionsList = new List<GameObject>();      GameObject choosedOption;          public void UserStart()  // this is called through a button        {            choosedOption = optionsList[Random.Range(0, optionsList.Count -1)];      }        }  
https://stackoverflow.com/questions/65964247/why-isnt-this-code-working-error-index-was-out-of-range-must-be-non-negative January 30, 2021 at 11:06AM

没有评论:

发表评论