Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 g0th1k4 · Jul 05, 2011 at 08:12 PM · arrayrangeindex

Array Index Out of Range

I'm having an array index out of range problem that I just can't seem to solve :S Does anyone have an idea? I've been looking it over and can't seem to locate the problem

 currentWaypointLocation = -1;

 var whichShell = ball.GetComponent("BallController").currentWaypointLocation;
 ball.GetComponent("BallController").SetPosition(shells[whichShell].transform.localPosition.x, shells[whichShell].transform.localPosition.z);

 ball.GetComponent("BallController").Show();


code addn


  //Start with the middle one - base all other positions off the middle
     shells[1].transform.localPosition.x = this.transform.localPosition.x;
     shells[1].transform.localPosition.z = this.transform.localPosition.z;
     
     shells[0].transform.localPosition.x = shells[1].transform.localPosition.x - .30;
     shells[0].transform.localPosition.z = shells[1].transform.localPosition.z;
     
     shells[2].transform.localPosition.x = shells[1].transform.localPosition.x + .30;
     shells[2].transform.localPosition.z = shells[1].transform.localPosition.z;
     
     //Starting 'numbers'
     shells[0].GetComponent("ShellController").curPositionNum = 0;
     shells[1].GetComponent("ShellController").curPositionNum = 1;
     shells[2].GetComponent("ShellController").curPositionNum = 2;
     
     //Set Reference Numbers -- For checking if user selects right shell later
     shells[0].GetComponent("ShellController").shellReference = 0;
     shells[1].GetComponent("ShellController").shellReference = 1;
     shells[2].GetComponent("ShellController").shellReference = 2;
     
     //Ball Stuff
     ball.GetComponent("BallController").currentWaypointLocation = Random.Range(0,299) % 3; //Pick a position for the ball to start in
     ball.transform.localPosition.x = shells[ ball.GetComponent("BallController").currentWaypointLocation ].transform.localPosition.x; //Set the position of the ball to whichever shell it is attached to
     ball.transform.localPosition.y = shells[ ball.GetComponent("BallController").currentWaypointLocation ].transform.localPosition.y;
     ball.transform.localPosition.z = shells[ ball.GetComponent("BallController").currentWaypointLocation ].transform.localPosition.z;

 for( var ii=0; ii < shells.length; ii++ )
     {
         //If we're the first object
         if( shells[ii].GetComponent("ShellController").curPositionNum == whichMove[0] )
         {
             //Set positions, and start moving
             shells[ii].GetComponent("ShellController").localStartPosition = shells[ii].transform.localPosition;
             shells[ii].GetComponent("ShellController").localEndPosition = shellPositionWaypoints[ whichMove[1] ];
             shells[ii].GetComponent("ShellController").endPositionNum = whichMove[1];
             shells[ii].GetComponent("ShellController").EnableMove();
         }
         
         if( shells[ii].GetComponent("ShellController").curPositionNum == whichMove[1] )
         {
             //Set positions, and start moving
             shells[ii].GetComponent("ShellController").localStartPosition = shells[ii].transform.localPosition;
             shells[ii].GetComponent("ShellController").localEndPosition = shellPositionWaypoints[ whichMove[0] ];
             shells[ii].GetComponent("ShellController").endPositionNum = whichMove[0];
             shells[ii].GetComponent("ShellController").EnableMove();
         }
     }    
     
     bAllReachedDestination = false; //No more setting coords, for now
 }
 
 
 //Stop moving shells when we hit the max number of moves
 function StopWhenMovesDone() : boolean
 {
     if( curMoveNumber >= numMovesThisRound )
     {        
         bRoundOver = true; //Round is over! NO MORE!
         bRoundChoice = true;
     
         //Make sure to put the Ball under the shell!
         var whichShell = ball.GetComponent("BallController").currentWaypointLocation;
         ball.GetComponent("BallController").SetPosition( shells[whichShell].transform.localPosition.x, shells[whichShell].transform.localPosition.z); //Set new position of whichever shell it's under!
         ball.GetComponent("BallController").Show(); //Make the ball visible again!
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Eric5h5 · Jul 05, 2011 at 08:18 PM

You're not actually showing any of the relevant code where this is used (also, use the code button to format code properly), but you can't use -1 as an array index.

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

Answer by SilverTabby · Jul 05, 2011 at 08:37 PM

Your error is that you are passing -1 into an array (currentWaypointLocation = -1)

Arrays can only accept int values in the range [0, arrayLength)

Also, you are calling GetComponent("String") too often. GetComponent is a very slow method, and passing a string to it makes it even slower. If you are calling your function in a tight loop, you will see a major performance drop.

I have edited your code (below) to avoid calling GetComponent more than needed, and to catch the situation where you pass an illegal index. Also, it looks nicer like this.

     currentWaypointLocation = 0;
     
     var controller : BallController = ball.GetComponent.<BallController>();
     var whichShell : int = controller.currentWaypointLocation;
 
     if(whichShell < 0 || whichShell >= shells.GetLength()  )
     {  
        Debug.Log("ArrayIndexOutOfBounds! Setting to default value...");
        whichShell = 0;
     }
 
     controller.SetPosition(shells[whichShell].transform.localPosition.x, shells[whichShell].transform.localPosition.z);
     controller.Show();
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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Arrays rebels to my power! 1 Answer

Array Index out of range? 1 Answer

Array index is out of Range!? 1 Answer

Index out of range exception - C# - Can't find the problem 1 Answer

What means this 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