Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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 /
avatar image
0
Question by YakDaddy · Aug 16, 2021 at 08:34 AM · listrandom

Argument Out Of Range for random number selector

This is what I am working on: I am currently creating a Bingo game. I have made a list with all possible numbers and assign the letter based on the number chosen.

This is the issue that I am having: after maybe 15 numbers are chosen I get this error and the whole thing freezes up. I'm not sure how it's trying to choose something beyond the scope of the list. It's not a negative number so I know it's not that.

Here is the code that I've come up with so far:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 
 public class NumberGenerator : MonoBehaviour
 {
     #region Variables
 
     [SerializeField] List<int> bingoNumberList;
     [SerializeField] List<int> calledNumberList;
 
     [SerializeField] Text calloutText;
 
     int currentNumber;
     char currentLetter;
 
     #endregion
 
     #region GameStart
 
     private void Start()
     {
         for (int i = 1; i < 76; i++)
         {
             bingoNumberList.Add(i);
         }
 
         StartCoroutine(PlayBingo());
     }
 
     IEnumerator PlayBingo()
     {
         while (bingoNumberList.Count > 0)
         {
             NumberSelection();
 
             yield return new WaitForSeconds(1f);
         }
 
         yield return null;
     }
 
     #endregion
 
     #region Core
 
     private void Update()
     {
         CalloutText();
     }
 
     private void NumberSelection()
     {
 
         if (bingoNumberList.Count > 0)
         {
             int randomIndex = bingoNumberList[Random.Range(0, bingoNumberList.Count)];
             currentNumber = bingoNumberList[randomIndex];
 
             LetterSelection();
 
             Debug.Log(currentNumber);
             Debug.Log(currentLetter);
 
             calledNumberList.Add(currentNumber);
             bingoNumberList.Remove(bingoNumberList[randomIndex]);
         }
         else
         {
             Debug.Log("All balls have been called!");
         }
     }
 
     private void LetterSelection()
     {
         if (currentNumber >= 1 && currentNumber <= 15)
         {
             currentLetter = 'B';
         }
         else if (currentNumber >= 16 && currentNumber <= 30)
         {
             currentLetter = 'I';
         }
         else if (currentNumber >= 31 && currentNumber <= 45)
         {
             currentLetter = 'N';
         }
         else if (currentNumber >= 46 && currentNumber <= 60)
         {
             currentLetter = 'G';
         }
         else
         {
             currentLetter = 'O';
         }
     }
 
     #endregion
 
     #region CalloutText
 
     public void CalloutText()
     {
         calloutText.text = "The current ball is " + currentLetter + currentNumber + "!";
     }
 
     #endregion
 }
 

Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Bunny83 · Aug 16, 2021 at 03:18 PM

Well, those two lines do not make much sense:

 int randomIndex = bingoNumberList[Random.Range(0, bingoNumberList.Count)];
 currentNumber = bingoNumberList[randomIndex];

Note that your "randomIndex" is not a random index into the bingoNumberList, but it's one of the elements of the bingoNumberList.

Just think about your logic for a moment. Imagine you have 5 elements

 // 1, 2, 3, 4, 5

You roll a random number between 0 and 4 and grab that element as index, say we roll a "2". That means you would remove element 2 from the array so your array would now look like this:

 // 1, 2, 4, 5

Since you use the picked element as index, this will fail when you pick a number that is higher than the remaining elements. For example if in the second run we randomly pick the last element (element with index 3 which has a value of "5"), you then would use the value (5) as index into that list. However the list only contains 4 elements at this point so the highest valid index is 3.


So you probably wanted to do:

 int randomIndex = Random.Range(0, bingoNumberList.Count);

instead. Also when you have the index of an element you want to remove, it's way better to use RemoveAt instead of Remove.

 bingoNumberList.RemoveAt(randomIndex);

Though that's not that important.

Comment
Add comment · Share
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
0

Answer by YakDaddy · Aug 16, 2021 at 03:24 PM

The way you have it is how I wrote it originally, but it was outputting the index and not the value at that index. That's why I was trying it this way.

I will go back and try again. Thank you

Comment
Add comment · Share
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

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

135 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 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

What range of values need to be inserted if we need to pull a random item from the list? 0 Answers

How do I get random answers? 2 Answers

Generate unique random numbers and sort them. 3 Answers

How would I implement a random spawn timer into a list based spawn system? 2 Answers

Scenes don't repeat themselves after pressing a restart button 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