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 /
avatar image
1
Question by darksblood · Jul 06, 2012 at 03:58 AM · listindexargument

Error: Argument is out of range Parameter name: index

Can someone help me understand what is going on here? Upon lauch, everything works fine and then after about a minute (sometimes longer), i get the error you see in the tittle. Please help, maybe there's something I'm not seing.

(ArgumentOutOfRangeException: Argument is out of range. Parameter name: index System.Collections.Generic.List`1[UnityEngine.GameObject].get_Item (Int32 index) Spawnhighway.Update () (at Assets/Scripts/Spawnhighway.cs:26)

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class Spawnhighway : MonoBehaviour 
 {
     public List<GameObject> Highways = new List<GameObject>();
     public float waittime = 3.0f;
     int highwayindex;
  
  
     GameObject thehighwayspawned;
     
     void Start()
     {
         highwayindex = Random.Range(1,8);
         thehighwayspawned = Instantiate((Highways)[highwayindex], new Vector3(10.44449f, transform.position.y, transform.position.z), Quaternion.identity) as GameObject;
     }
 
     void Update()
     { 
         
         if ( thehighwayspawned.transform.position.x <= 2.368699)
         {
             int highwayindexx = Random.Range(1,8);
             thehighwayspawned = Instantiate((Highways)[highwayindexx], new Vector3(9.686185f, transform.position.y, transform.position.z), Quaternion.identity) as GameObject;
         }
     }
 
   
 }
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 mohanrao164 · Jul 06, 2012 at 06:43 AM 0
Share

check out with the if statement u cannot compare gameobject with int so because of that it is displaying index out of range

avatar image whydoidoit · Jul 06, 2012 at 08:00 AM 0
Share

I've formatted your code for you - please indent it by 4 spaces or 1 tab before pasting, or highlight it and then press the code button after pasting.

1 Reply

· Add your reply
  • Sort: 
avatar image
4

Answer by Kryptos · Jul 06, 2012 at 07:58 AM

To properly use Random.Range, you should always use the current size of the collection (in your case the List).

 highwayindex = Random.Range(0, Highways.Count);

Also make sure that your list is filled with enough values (in your case 8), before the game start.

Comment
Add comment · Show 2 · 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 flamy · Jul 06, 2012 at 09:10 AM 0
Share

oh my bad didnt check it properly, i saw the doc for float and got hasty =/.

deleted my answer.

avatar image darksblood · Jul 06, 2012 at 12:37 PM 0
Share

Thank you, I've been running the test for about 15 $$anonymous$$utes now and it hasn't stopped the game to display the error where as before it would have done so after about a $$anonymous$$ute.

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

9 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Find the correct index number for array inside two loops 2 Answers

A node in a childnode? 1 Answer

What type of array should I use to handle a convoy? 0 Answers

List<> Size Changes Without Removing Elements 1 Answer

Assign a GameObject to one from a List<> 2 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