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 cyoder · Apr 05, 2013 at 06:37 AM · arrayarraysindexoutofrangeexception

IndexOutOfRangeException error for array points in range

I've created a Vector3 array with the length of four with this code (these are declared globally):

 var contactpointSide : Vector3[] = new Vector3[4];
 var contactnormalSide : Vector3[] = new Vector3[4];

and the array size in the inspector shows 4. Whenever I run the code, though, the size of the array changes to 0, and when I try to add vectors to the array, it gives me an IndexOutOfRangeException. Nowhere in the code do I modify or do anything related to the length of the vector, so I don't know why the vector size keeps changing to zero. Can anyone spot my problem or have any ideas?

Here's the code, with the offending lines marked (with unrelated stuff removed):

 function OnCollisionStay (collision : Collision)
 {
     for (var contact : ContactPoint in collision.contacts)
     {
         
 ...
         
         if(dotprod >= -0.5 && rotationprod > 0.7) // left
                 {
             // stuff here
                         
    //THIS LINE//contactpointSide[0] = contact.point;
    //THIS TOO// contactnormalSide[0] = contact.normal;
             
                 }
         }
 }

Comment
Add comment · Show 1
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 Chronos-L · Apr 05, 2013 at 06:44 AM 0
Share

Did you make changes to the contactpointSide and contactnormalSide in any other part of your code?

1 Reply

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

Answer by Chronos-L · Apr 05, 2013 at 06:49 AM

 #pragma strict
 var contactpointSide : Vector3[] = new Vector3[4];
 var contactnormalSide : Vector3[] = new Vector3[4];
 
 function OnCollisionStay (collision : Collision)
 {
     for (var contact : ContactPoint in collision.contacts)
     {
             // These 2 lines produce errors in your script 
             // because they have become null when 
             // this segment is executed
         contactpointSide[0] = contact.point;
         contactnormalSide[0] = contact.normal;
     }
 }

I ran this with the collider/rigidbody properly setup, and I can see contactpointsSide and contactnormalSide updates to new values when the rigidbody this script is attached to dropped onto and collide with a plane.

Comment
Add comment · Show 5 · 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 cyoder · Apr 05, 2013 at 11:25 AM 0
Share

Okay, thanks for checking it out. Do you know why they have become null or how to code a workaround? I'm wanting to put different collision information into an array to pass to other functions.

avatar image Chronos-L · Apr 05, 2013 at 11:48 AM 0
Share

why they have become null

You made them to (Whenever I run the code, though, the size of the array changes to 0), that's the only reason I can think of. I could be wrong, if anyone's opinion differs from $$anonymous$$e, speak up.

how to code a workaround

Don't make them become null. Post your script here (the whole script, if it is not possible, just keep codes that deal with the arrays. We will see if we can find out which line is causing the problem.

avatar image cyoder · Apr 05, 2013 at 07:12 PM 0
Share

I actually don't change them via code at all, they just change by themselves whenever I collide with something. The lines in the code above are the only two places where I even have the contactpointSide or contactnormalSide variables mentioned, so I don't know why it's beco$$anonymous$$g null.

I'd be happy to post the code, but it's pretty long. Should I post it all here?

avatar image Chronos-L · Apr 06, 2013 at 12:07 AM 0
Share

When I run the code in my answer (a very short version of yours), it works out alright, the only explanation I can think of is that your other code have changed the arrays.

Are you sure that you did not use/meddle with the arrays in your script? Do you $$anonymous$$d trying Search->Find-in-Files (Ctrl-Shift-F) on your project solution just to be sure?

avatar image cyoder · Apr 06, 2013 at 02:16 PM 0
Share

Okay, I actually coded a work around. For some reason, whenever OnCollisionStay was called, it would reset the vector. I still don't know why, because I didn't modify the length in code, but I found out that declaring or redeclaring the length in Start kept the length when OnCollisionStay was called.

Thanks for the help, Chronos-L! I really appreciate the time you put into helping me out.

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

11 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

Related Questions

Index is Less Than Zero + Flawed Code 1 Answer

Array index is out of range 0 Answers

IndexOutofRangeException 1 Answer

my QandA array is not working when you choose 3 wrong answers 1 Answer

Array empties values on RunTime? 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