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 goolygot · Apr 30, 2012 at 03:37 PM · javascripterrorvariable

Weird error every frame

I get a weird error every frame. I know what gives the error, but I have no clue why it gives it that error. The object this script moves like it is meant to do, with the no error in the moving.

This is the one that gives the error:

waypoints[currentWP].position

This is the error:

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

Parameter name: index 0 System.Collections.ArrayList.ThrowNewArgumentOutOfRangeException (System.String name, System.Object actual, System.String message) System.Collections.ArrayList.get_Item (Int32 index) UnityScript.Lang.Array.get_Item (Int32 index) EnemyMove: The script that produces the error var waypoints = new Array (); var currentWP : int; var moveSpeed : float = 1.0; var rotSpeed : float = 25.0; var damping = 6.0;
function Update () {
transform.position = Vector3.MoveTowards(transform.position, waypoints[currentWP].position, moveSpeed Time.deltaTime);
var rotation = Quaternion.LookRotation(waypoints[currentWP].position - transform.position); transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime
damping); }
function OnTriggerEnter (other : Collider) { for (var waypoint : Transform in waypoints){ if (other.name == waypoint.name){ currentWP = parseInt (other.name); } } } EnemyLane: The script that gives the variable to the other script static var waypoints : Transform[]; var prefab : Transform; static var prefab2 : Transform; static var laneLevel : int = 1; var monsterType1 : String[]; var monsterSize1 : int[];
function Awake () { waypoints = new Transform[transform.childCount]; prefab2 = prefab; var test : int; for (var child : Transform in transform) { test = parseInt (child.name) - 1; waypoints[test] = child; } }
function SendLane (laneLevel : int, monsterType : String[], monsterSize : int[]) { // Find the right enemy prefab in the RESOURCES folder.
// First enemy: monsterType[0] = monsterSize[0] var count : int; var timer : float;
while (count < monsterSize[0]) { SpawnMonster (timer, prefab2); count ++; timer += 1.0; } }
function Start (){ SendLane (laneLevel, monsterType1, monsterSize1); }
function SpawnMonster (waiter : int, monsterPrefab : Transform){ yield WaitForSeconds (waiter); var monster = Instantiate (monsterPrefab, waypoints[0].position, Quaternion.identity); var other : EnemyMove = monster.gameObject.GetComponent(EnemyMove); var test : int = 0; other.waypoints = waypoints;
}
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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Statement · Apr 30, 2012 at 03:57 PM

"Index is less than 0 or more than or equal to the list count."

It means that currentWP is less than 0 (-1, -30, -38 etc), or more than or equal to the list count (if waypoints contains 5 items, currentWP 5, 6, 7, 10, 2000 are invalid indices).

To translate to english: If you have five items, 0 to 4, which is item -1? Likewise, which is item 5? It doesn't exist.

Use the debugger to step through your code to see if your array is large enough for the indices you use. If not, find out why.

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 goolygot · May 01, 2012 at 03:34 PM 0
Share

But if the index is wrong, why does the object find the right position to move to? It does everything as it should, just that it produces this error, that I still can't figure out how to remove. Because the index is never out of range of the array.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Scripting error! 1 Answer

When i call some variables to an other second script i have a problem 1 Answer

What is the problem with this code? 1 Answer

"Expecting ), found ';'" and "';' expected. Insert a semicolon at the end" 1 Answer

How would I be able to subtract from a variable when it hits something? (OnCollisionEnter) 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