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
0
Question by SamChietGames · Apr 10, 2014 at 03:00 AM · foreachforargumentoutofrangeexception

For loop not repeating - (JAVASCRIPT)

Hello, everyone. I'm working on a memry card game and using the "iphone Game 4.0" code as a base. (it's a memory card game...)

Whenever I run the code, it doesn't create the cards because of this error:

 ArgumentOutOfRangeException: Index is less than 0 or more than or equal to the list count.
 Parameter name: index
 

I've looked on the forums and cannot find a satisfactory answer - I've even copy-and-pasted the code from the old script intO my project file.

Here is the code:

 #pragma strict
 
 var cardLocations =    new Array 
 ( 
     Vector3 (0, 0, 0), Vector3(3, 0, 0), Vector3(6, 0, 0), Vector3(9, 0, 0), Vector3(12, 0, 0), Vector3(15, 0, 0),
     Vector3 (0, 0, -4), Vector3(3, 0, -4), Vector3(6, 0, -4), Vector3(9, 0, -4), Vector3(12, 0, -4), Vector3(15, 0, -4),
     Vector3 (0, 0, -8), Vector3(3, 0, -8), Vector3(6, 0, -8), Vector3(9, 0, -8), Vector3(12, 0, -8), Vector3(15, 0, -8)
 );
 
 var allCards : GameObject;
 var parentObject : GameObject;
 
 var usedCardLocation;
 
 var uniqueCard = new Array ();
 
 
 function Start () { 
 {    
     // Find all Game Objects that are Tagged "Cards"...
     
     uniqueCard = allCards.FindGameObjectsWithTag ("Cards");
     
     // Randomize the order of the Cards...
     
     for ( var c=0; c <= uniqueCard.length-1; c++)
     {
         var thisCard = Random.Range (c, uniqueCard.length);
     
         usedCardLocation = uniqueCard [c];
         
         uniqueCard [c] = uniqueCard [thisCard];
         
         uniqueCard [thisCard] = usedCardLocation;
     }
     
     // Randomize the order of the Card Locations...
     
     for ( var i=0; i <= cardLocations.length-1; i++)
     {
         var thisCardLocation = Random.Range (i, cardLocations.length);
         
         usedCardLocation = cardLocations [i];
         
         cardLocations [i] = cardLocations [thisCardLocation];
         
         cardLocations [thisCardLocation] = usedCardLocation;
         
         // Instantiate our Randomized Cards and parent them to empty Game Objects so the animations play in Local Space...
         
         var theCard : GameObject = Instantiate (uniqueCard [i/2], Vector3(0,0,0), Quaternion.identity);
             
         var theParent : GameObject = Instantiate (parentObject, Vector3(0,0,0) , Quaternion.identity);
         
         theCard.transform.parent = theParent.transform;
 
         
         theParent.transform.position = cardLocations [i];
     }
     
     // Destroy the group of cards that we're using to generate the Card Locations...
 Destroy(allCards, 0.1);
 
 
 }
 }

Can somebody please help me?

Thanks!

-Sam

Comment
Add comment · Show 3
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 ToxxicSin · Apr 10, 2014 at 03:15 AM 0
Share

I believe the index that is out of range isn't an issue with the for loop itself. Your issue is where you're using your iterator to call a value. It's trying to use an index that doesn't exist in one of the arrays It's just some simple logic. Just run through all of your code and think hard about the possible size of each array, and see if the 'i' or 'c' is larger than a possible index in each array. I would search myself, but I'm too tired to find that.

avatar image robertbu · Apr 10, 2014 at 05:19 AM 0
Share

Can you past in the error message from the console, and/or indicate which line is giving you the error? Note the Array class is to be avoided. Use a built-in array ins$$anonymous$$d.

avatar image GrahamReeves ♦♦ · Apr 10, 2014 at 09:09 AM 0
Share

This WILL throw this exception if uniqueCard.length is zero, or I think, less than half the size of cardLocations. Perhaps that array isn't filling up in the first place. Do a debug.log( uniqueCard.length ); and see if it's found all the game objects okay

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by GrahamReeves · Apr 10, 2014 at 09:40 AM

I created a scene with a parent object, and 3 cubes tagged with "Cards" and dropped your code in and it stopped with "parentObject is unassigned".

If I removed the Cards tags, I get the error ArgumentOutOfRangeException like you do. So perhaps you don't have the tags setup correctly? link text


cardshuffle.zip (105.1 kB)
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

24 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

Related Questions

How do i use foreach/for loops properly? 1 Answer

my wave spawner not working properly 1 Answer

Is having a "break" in a "foreach" loop that is itself inside a "for" loop breaking both loops? 3 Answers

Unity Is Caling My Vector3 a Float? 2 Answers

Why is it trying to grab the wrong rigidbody? 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