Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
This question was closed Sep 11, 2016 at 01:30 AM by Eagle-Bound for the following reason:

Other

avatar image
0
Question by Eagle-Bound · Sep 08, 2016 at 09:26 PM · c#playerprefsnoobscore system

Argument out of range

In my really simple simon says game, everything used to be working fine. The game would generate a pattern, and when the player got the pattern correct, the computer would give the same thing but add another color. However, when I went to add a high score system, basically everything broke. On the second round of a pattern, the first color would not repeat, and I get an error that says 'Argument out of range'. Its probably something really easy to fix, I don't see anything wrong with it, but if anyone else does, I would really appreciate it. My script:

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine.UI;
 
 public class GameManager : MonoBehaviour {
 
     public SpriteRenderer[] colors;
 
     public AudioSource[] buttonSounds;
 
     private int colorSelect;
 
     public float lit;
 
     private float litCounter;
 
     public float waitBetweenLight;
 
     private float waitBetweenCounter;
 
     private bool shouldBeLit;
 
     private bool shouldBeDim;
 
     public List <int> activeSequence;
 
     private int posInSequence;
 
     private bool gameActive;
 
     private int inputInSequence;
 
     public AudioSource correct;
 
     public AudioSource incorrect;
 
     public Text scoreText;
 
     // Use this for initialization
     void Start () {
 
         if (!PlayerPrefs.HasKey ("High Score")) {
             PlayerPrefs.SetInt ("High Score", 0);
         }
 
         scoreText.text = "Score: 0 - High Score: " + PlayerPrefs.GetInt("High Score");
 
     }
     
     // Update is called once per frame
     void Update () {
 
         if (shouldBeLit) {
             litCounter -= Time.deltaTime;
             if (litCounter < 0) {
                 colors [activeSequence[posInSequence]].color = new Color (colors [activeSequence[posInSequence]].color.r, colors [activeSequence[posInSequence]].color.g, colors [activeSequence[posInSequence]].color.b, 0.5f);
                 buttonSounds [activeSequence [posInSequence]].Stop();
                 shouldBeLit = false;
 
                 shouldBeDim = true;
                 waitBetweenCounter = waitBetweenLight;
 
                 posInSequence++;
             }
         }
         if (shouldBeDim) {
             waitBetweenCounter -= Time.deltaTime;
 
             if (posInSequence >= activeSequence.Count) {
 
                 shouldBeDim = false;
                 gameActive = true;
 
             } else {
                 if (waitBetweenCounter > 0) {
                     
                     /*colorSelect = Random.Range (0, colors.Length);
 
                     activeSequence.Add (colorSelect);*/
 
                     colors [activeSequence[posInSequence]].color = new Color (colors [activeSequence[posInSequence]].color.r, colors [activeSequence[posInSequence]].color.g, colors [activeSequence[posInSequence]].color.b, 1f);
 
                     buttonSounds [activeSequence [posInSequence]].Play();
 
                     litCounter = lit;
                     shouldBeLit = true;
                     shouldBeDim = false;
                 }
             }
         }
     }
 
     public void StartGame () {
 
         activeSequence.Clear ();
 
         posInSequence = 0;
 
         colorSelect = Random.Range (0, colors.Length);
 
         activeSequence.Add (colorSelect);
 
         colors [activeSequence[posInSequence]].color = new Color (colors [activeSequence[posInSequence]].color.r, colors [activeSequence[posInSequence]].color.g, colors [activeSequence[posInSequence]].color.b, 1f);
 
         buttonSounds [activeSequence [posInSequence]].Play();
 
         litCounter = lit;
         shouldBeLit = true;
 
         scoreText.text = "Score: 0 - High Score: " + PlayerPrefs.GetInt("High Score");
     }
 
     public void ColourPressed(int whichButton){
 
         if (gameActive) {
 
 
 
             if (activeSequence[inputInSequence] == whichButton) {
                 Debug.Log ("Correct");
 
                 inputInSequence++;
 
                 if (inputInSequence >= activeSequence.Count) {
 
                     PlayerPrefs.SetInt ("High Score", activeSequence.Count);
                 }
 
                     if (activeSequence.Count > PlayerPrefs.GetInt("High Score")){
 
                     scoreText.text = "Score: " + activeSequence.Count + "High Score" + PlayerPrefs.GetInt("High Score");
 
                     posInSequence = 0;
 
                     inputInSequence = 0;
 
                     colorSelect = Random.Range (0, colors.Length);
 
                     activeSequence.Add (colorSelect);
 
                     colors [activeSequence[posInSequence]].color = new Color (colors [activeSequence[posInSequence]].color.r, colors [activeSequence[posInSequence]].color.g, colors [activeSequence[posInSequence]].color.b, 1f);
 
                     buttonSounds [activeSequence [posInSequence]].Play(); 
 
                     litCounter = lit;
                     shouldBeLit = true;
 
                     gameActive = false;
 
                     correct.Play ();
 
                 }
 
             } else {
                 Debug.Log ("Wrong");
                 incorrect.Play ();
                 gameActive = false;
             }
         }
 
     }
 }

Edit: So I re-typed the code , and I'm not sure what was wrong, but it fixed itself.

Comment
Add comment · Show 2
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 Rob2309 · Sep 09, 2016 at 10:00 PM 0
Share

Could you give the line of the error? :)

avatar image tanoshimi · Sep 09, 2016 at 10:36 PM 1
Share

and how many elements have you assigned to the colors and buttonSounds arrays?

1 Reply

  • Sort: 
avatar image
-1

Answer by Cynikal · Sep 09, 2016 at 10:12 PM

I'm going to assume it's because of your things like:

colorSelect = Random.Range (0, colors.Length);

Since you're using it in: whatever[colorSelect] style, you should be using:

colorSelect = Random.Range (0, colors.Count - 1);

It's out of range, because you're most likely pulling Max Range + 1.

If you have 30 variables in an array, and you use Random.Range(0, whatever.Length/Count/Whatever), it can return: 30.. However, when pulling from an array, the max is 29, since arrays start at 0.

Comment
Add comment · Show 1 · 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 tanoshimi · Sep 09, 2016 at 10:37 PM 2
Share

No, because Random.Range is exclusive of the max value.

Follow this Question

Answers Answers and Comments

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

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

counting score after destroying game object 1 Answer

How to remember the level/scene? 1 Answer

How to load a scene with playerprefs from a ui element 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