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 kiko · Aug 04, 2011 at 04:35 PM · arrayarray-out-of-range-except

problem with array index out of range

hi guys i trying to make my player object to collide with the cube object which by right will print out laps 1 on the race track .but there an error which said "array index is out of range"and i can't figure out why can anyone help me?

here is the script for cube object:

 function OnTriggerEnter (other : Collider) {
     //Is it the Player who enters the collider?
     if (!other.CompareTag("Player")) 
         return; //If it's not the player dont continue
         
     //Is this transform equal to the transform of checkpointArrays[currentCheckpoint]?
     if (transform==playerTransform.GetComponent(player).checkPointArray[player.currentCheckpoint].transform) {
         //Check so we dont exceed our checkpoint quantity
         if (player.currentCheckpoint+1<playerTransform.GetComponent(player).checkPointArray.length) {
             //Add to currentLap if currentCheckpoint is 0
             if(player.currentCheckpoint==0)
                 player.currentLap++;
             player.currentCheckpoint++;
         } else {
             //If we dont have any Checkpoints left, go back to 0
             player.currentCheckpoint=0;
         }
         visualAid(); //Run a coroutine to update the visual aid of our Checkpoints
         //Update the 3dtext
         Camera.main.GetComponentInChildren(TextMesh).text = "Checkpoint: "+(player.currentCheckpoint)+" Lap: "+(player.currentLap);
     }
 }
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 Jake-L · Aug 05, 2011 at 02:18 PM 0
Share

None else can't figure it out without seeing your project. Just make sure that player.currentCheckpoint is in the array range of checkPointArray.

avatar image SisterKy · Aug 06, 2011 at 09:03 AM 0
Share

please don't invent a new tag (array-out-range-except) when there's a perfect equivalent already in existence (array-out-of-range-except). Thank you.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by aldonaletto · Aug 05, 2011 at 03:23 PM

I could not find any error in your script, since it already checks the array length before incrementing currentCheckpoint. I only found somewhat weird the use of GetComponent to access checkPointArray - since currentCheckpoint is a static var, I suppose checkPointArray is static too. Is it true? I would make checkPointArray static, then access it using player.checkPointArray, like below:

    if (transform == player.checkPointArray[player.currentCheckpoint].transform) {
        //Check so we dont exceed our checkpoint quantity
        if (player.currentCheckpoint+1
I already had some trouble with complicated constructions using GetComponent, so I try to simplify them whenever possible.
Another possible cause for this error is some other script altering currentCheckpoint.
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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

IndexOutOfRange error on iPhone, but not on PC/Player 0 Answers

Array index is out of range 1 Answer

IndexOutOfRangeException: Array index is out of range. 2 Answers

Find if no objects with tag exist 3 Answers

Instantiating random prefabs 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