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 DEHYDRATED_GOAT · Sep 01, 2020 at 02:05 AM · errorerror messageargumentoutofrangeexception

Argument out of Range Exception Error. [Basic]

Hello, I am attempting to follow along with a unity tutorial to create a basic free-runner game. Within my script written for a basic object pooling ground generator I meet no apparent errors within my script. For context, this script is attached to an empty game object that should auto generate prefab platforms, moving forward and randomize designated obstacle's appearance. However, upon launching the scene I am met with this error inside unity's console:

  ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.
 Parameter name: index
 System.ThrowHelper.ThrowArgumentOutOfRangeException (System.ExceptionArgument argument, System.ExceptionResource resource) (at <fb001e01371b4adca20013e0ac763896>:0)
 System.ThrowHelper.ThrowArgumentOutOfRangeException () (at <fb001e01371b4adca20013e0ac763896>:0)
 GroundGenerator.Update () (at Assets/Scripts/GroundGenerator.cs:88)

I have researched this error and understand that this is a common error and is related to my use of arrays, however in studying past solutions to this error I am struggling to convert it into solving my issue. Furthermore, I have compared my script with that provided by the tutorial (link) and cannot see any apparent differences that generate this error. Moreover, I have followed the console's error pointing to line 88, however that line is simply a }.

Apologies if this is a simple fix that I'm easily missing and wasting forum discussion space, this is only my second week working with unity and C#, and after 2 days staring at this error, I'm only becoming more confused. I highly appreciate any insight anyone can provide into how to correct this error.

my code:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEditor;
 using UnityEngine;
 using UnityEngine.SceneManagement;
 
 public class GroundGenerator : MonoBehaviour
 {
     public Camera mainCamera;
     public Transform startPoint; // point from where ground tile will begin 
     public PlatformTile tilePrefab;
     public float movingSpeed = 12;
     public int tilestoPreSpawn = 15; // how many tiles should be pre-spawned
     public int tilesWithoutObstacles = 3; //How many tiles at the beginning should not have obstacles, good for warm-up 
 
     List<PlatformTile> spawnedTiles = new List<PlatformTile>();
     int nextTileToActivate = -1;
     [HideInInspector]
     public bool gameOver = false;
     static bool gameStarted = false;
     float score = 0;
 
     public static GroundGenerator instance;
 
     void Start()
     {
         instance = this;
 
         Vector3 spawnPosition = startPoint.position;
         int tilesWithNoObstaclesTmp = tilesWithoutObstacles;
         for (int i = 0; i < tilestoPreSpawn; i++)
         {
             spawnPosition -= tilePrefab.startPoint.localPosition;
             PlatformTile spawnedTile = Instantiate(tilePrefab, spawnPosition, Quaternion.identity) as PlatformTile;
             if (tilesWithNoObstaclesTmp > 0)
             {
                 spawnedTile.DeactivateAllObstacles();
                 tilesWithNoObstaclesTmp--;
             }
             else
             {
                 spawnedTile.ActivateRandomObstacle();
             }
 
             spawnPosition = spawnedTile.endPoint.position;
             spawnedTile.transform.SetParent(transform);
             spawnedTiles.Add(spawnedTile);
         }
     }
     void Update()
     {
         //moves the object upward in world space * unit/second
         // increasing speed increases the score
         if (!gameOver && gameStarted)
         {
             transform.Translate(-spawnedTiles[0].transform.forward * Time.deltaTime * (movingSpeed + (score / 500)), Space.World);
             score += Time.deltaTime * movingSpeed;
         }
 
         if (mainCamera.WorldToViewportPoint(spawnedTiles[0].endPoint.position).z < 0)
         {
             // move the tile to the front if it's being the camera
             PlatformTile tiletmp = spawnedTiles[0];
             spawnedTiles.RemoveAt(0);
             tiletmp.transform.position = spawnedTiles[spawnedTiles.Count - 1].endPoint.position - tiletmp.startPoint.localPosition;
             tiletmp.ActivateRandomObstacle();
             spawnedTiles.Add(tiletmp);
         }
 
         if (gameOver || !gameStarted)
         {
             if (Input.GetKeyDown(KeyCode.Space))
             {
                 if (gameOver)
                 {
                     //restart current scene
                     Scene scene = SceneManager.GetActiveScene();
                     SceneManager.LoadScene(scene.name);
                 }
                 else
                 {
                     //start the game 
                     gameStarted = true;
                 }
             }
         }
 
     }
         void OnGUI()
         {
             if (gameOver)
             {
                 GUI.color = Color.red;
                 GUI.Label(new Rect(Screen.width / 2 - 100, Screen.height / 2 - 100, 200, 200), "Game Over\nYour score is: " + ((int)score) + "\nPress 'Space' to restart");
             }
             else
             {
                 if (!gameStarted)
                 {
                     GUI.color = Color.red;
                     GUI.Label(new Rect(Screen.width / 2 - 100, Screen.height / 2 - 100, 200, 200), "Press 'Space' to start");
                 }
             }
 
 
             GUI.color = Color.green;
             GUI.Label(new Rect(5, 5, 200, 25), "Score: " + ((int)score));
         }
     }


Link to the tutorial I am following

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

0 Replies

· Add your reply
  • Sort: 

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

254 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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

0xc000007b error on 64 bit machine? 1 Answer

2018.2 text mesh pro. plugin Errors. 7 Answers

Assertion failed on expression: 'channel.frameCount != 0' ,Assertion failed on expression: 'channel.frameCount != 0' - 2018.3.3f1 6 Answers

AndroidArchitecture' does not contain a definition for 'X86' 1 Answer

PLEASE HELP, BUILD APK ERROR 0 Answers


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