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 Tomas_D · Feb 09, 2014 at 06:41 PM · pokemon

ArgumentOutOfRangeException: Argument is out of range. Parameter name: index

I currently working thought this guys youtube tutorial series "Lets clone a Pokemon Game"Tutorial and I keep getting this "ArgumentOutOfRangeException: Argument is out of range. Parameter name: index" And for the life of me I can't figure out why? As far as I can tell my code looks just like his.

 #pragma strict
 
 import System.Collections.Generic;
 
 //All Enemies
 
 var AllMonsters : monster[];
 
 var enemyMonster : monster;
 
 var monsterEquipped : monster;
 
 function Start () {
 
 }
 
 function OnGUI ()
 {
 
     var other : Player_stats;
     other = gameObject.Find("_Player").GetComponent(Player_stats);
     
     if(other.isInCombat)
     {
         GUI.Label (Rect(50, 100, 200, 100), ""+enemyMonster.name);
         GUI.DrawTexture(Rect(70, 100, 128, 128), enemyMonster.image);
     }
     
 }
 
 function Update () 
 {
     
 }
 
 function randomizeMonster ()
 {
     var other : Player_stats;
     other = gameObject.Find("_Player").GetComponent(Player_stats);
     
     var tempMonsters : List.<monster> = new List. <monster>();
     var randomNum : int = Random.Range(0,100);
     
     if(randomNum == 20)
     {
         for(var i = 0; i < AllMonsters.Length; i++)
         {
             if (AllMonsters[i].rarity == Rarity.rare && AllMonsters[i].regionLocated == other.region)
             {
                 tempMonsters.Add(AllMonsters[i]);
             }
         }
     }
     else
     {
         for(var j = 0; j < AllMonsters.Length;j++)
         {
             if(AllMonsters[j].rarity == Rarity.common && AllMonsters[j].regionLocated == other.region)
             {
                 tempMonsters.Add(AllMonsters[j]);
             }
         }
     }
     
     var newRandom = Random.Range(0, tempMonsters.Count);
     enemyMonster = tempMonsters[newRandom];
 }


Comment
Add comment · Show 4
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 robertbu · Feb 09, 2014 at 06:44 PM 0
Share

Please post the error message and/or indicate what line is generating the error.

avatar image Tomas_D · Feb 09, 2014 at 09:06 PM 0
Share

The error message is the title of this post, thats all it shows. After using comments I isolated the problem down to this line at the bottom.

 enemy$$anonymous$$onster = temp$$anonymous$$onsters[newRandom];
avatar image DajBuzi · Feb 09, 2014 at 09:14 PM 0
Share

Try to debug temp$$anonymous$$onsters.Count and newRandom

 Debug.Log(temp$$anonymous$$onsters.Count());
 var newRandom = Random.Range(0, temp$$anonymous$$onsters.Count());
 Debug.Log(newRandom);
avatar image Tomas_D · Feb 09, 2014 at 10:13 PM 0
Share

For

Debug.Log(temp$$anonymous$$onsters.Count());

I just get a value of zero every time. And the other Debug for newRandom doesn't even get called.

3 Replies

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

Answer by zharik86 · Feb 09, 2014 at 10:36 PM

@giano574 Everything speaks correctly. Your error is that you have an empty list (just it is visible from logs). In your list there is no element. The most correct that it is possible to make, it in case of initialization to add an element, for example:

   var tempMonsters : List.<monster> = new List. <monster>();
   tempMonsters.Add(AllMonsters[0]);

Or to do any check on existence of elements in the list: tempMonsters.Count != 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 Tomas_D · Feb 09, 2014 at 11:03 PM 0
Share

You were right there were no elements in the list, because my dumb self for got that I changed the region variable from and int to a string and never updated that in the inspector.

thanks everyone for your time.

avatar image
1

Answer by giano574 · Feb 09, 2014 at 09:19 PM

The problem is that you are creating a random between 0 and tempMonsters.Count. If the array is say, 3 long, it will look like this:

 [0] = 1
 [1] = 2
 [2] = 3

In this case tempMonsters.Count will return 3. If you look for something at the index 3 in tempMonsters you will get an error, because there is nothing at this index. Change your code to:

 var newRandom = Random.Range(0, tempMonsters.Count - 1);
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 giano574 · Feb 09, 2014 at 09:31 PM 0
Share

Oh, I seem to be mistaken. According to the documentation Random.Range will never return the max value. http://docs.unity3d.com/Documentation/ScriptReference/Random.Range.html

You should try debugging the temp$$anonymous$$onsters.Count and newRandom, like the others say.

avatar image
0

Answer by WarpZone · Dec 01, 2016 at 01:35 PM

I get this error in a blank project. Why?

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

22 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

Related Questions

Multiple Cars not working 1 Answer

I made a better shader how do i fix[add _Shadow Strength]help???>Sorry that im asking for to much 1 Answer

Help In Making a SphereCast for 3D Tire! Working RayCast Script included! 0 Answers

UnityEngine.Input.GetMouseButton(1)) issue 1 Answer

BCE0044 Error 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