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 Reefer · Nov 24, 2013 at 01:17 AM · errorcardamagedeforming

C# CarDamage script: Array index is out of range error.

So the problem is that I have an car damage script, which deforms mesh on collision. It works but it keeps giving me this error and I don't understand what's wrong it. So if someone could point me on right direction.

ERROR:

IndexOutOfRangeException: Array index is out of range. CarDamage2.OnCollisionEnter (UnityEngine.Collision collision) (at Assets/Scripts/CarDamage2.cs:44)

SCRIPT:

 using UnityEngine;
 using System.Collections;
 
 public class CarDamage2 : MonoBehaviour 
 {
     public float maxMoveDelta = 1.0f; // maximum distance one vertice moves per explosion (in meters)
     public float maxCollisionStrength = 50.0f;
     public float YforceDamp = 0.1f; // 0.0 - 1.0
     public float demolutionRange = 0.5f;
     public float impactDirManipulator = 0.0f;
     public MeshFilter[] optionalMeshList;
     
     private MeshFilter[] meshfilters;
     private float sqrDemRange;
 
     public void Start()
     {
         //maxMoveDelta = Mathf.Clamp01(maxMoveDelta);
        
         
         if(optionalMeshList.Length>0)
             meshfilters = optionalMeshList;
         else
             meshfilters = GetComponentsInChildren<MeshFilter>();
             
         sqrDemRange = demolutionRange*demolutionRange;
             
         /* doesnt work, strange physic behaviour
         for(int j=0; j<meshfilters.Length; ++j)
         {
                if(!meshfilters[j].transform.collider)
                 {
                 meshfilters[j].gameObject.AddComponent<BoxCollider>();        
                 meshfilters[j].transform.collider.isTrigger = true;    
             }
         }*/
     }
     
     public void OnCollisionEnter( Collision collision ) 
     {
         Vector3 colRelVel = collision.relativeVelocity;
         colRelVel.y *= YforceDamp;
         
         Vector3 colPointToMe = transform.position - collision.contacts[0].point;
 
         // Dot = angle to collision point, frontal = highest damage, strip = lowest damage
         float colStrength = colRelVel.magnitude * Vector3.Dot(collision.contacts[0].normal, colPointToMe.normalized);
 
         OnMeshForce( collision.contacts[0].point, Mathf.Clamp01(colStrength/maxCollisionStrength) );
         Debug.DrawLine(collision.contacts[0].point, transform.position, Color.red);
         //Debug.Break();
     }
     
     // if called by SendMessage(), we only have 1 param
     public void OnMeshForce( Vector4 originPosAndForce )
     {
         OnMeshForce( (Vector3)originPosAndForce, originPosAndForce.w );
     }
 
     public void OnMeshForce( Vector3 originPos, float force )
     {
         // force should be between 0.0 and 1.0
         force = Mathf.Clamp01(force);
         
         for(int j=0; j<meshfilters.Length; ++j)
         {
             Vector3 [] verts = meshfilters[j].mesh.vertices;
 
             for (int i=0;i<verts.Length;++i)
             {
                 Vector3 scaledVert = Vector3.Scale( verts[i], transform.localScale );                        
                 Vector3 vertWorldPos = meshfilters[j].transform.position + (meshfilters[j].transform.rotation * scaledVert);
                 Vector3 originToMeDir = vertWorldPos - originPos;
                 Vector3 flatVertToCenterDir = transform.position - vertWorldPos;
                 flatVertToCenterDir.y = 0.0f;
                    
                 // 0.5 - 1 => 45° to 0°  / current vertice is nearer to exploPos than center of bounds
                 if( originToMeDir.sqrMagnitude < sqrDemRange ) //dot > 0.8f )
                 {
                     float dist = Mathf.Clamp01(originToMeDir.sqrMagnitude/sqrDemRange);
                     float moveDelta = force * (1.0f-dist) * maxMoveDelta;
 
                     Vector3 moveDir = Vector3.Slerp(originToMeDir, flatVertToCenterDir, impactDirManipulator).normalized * moveDelta;
 
                     verts[i] += Quaternion.Inverse(transform.rotation)*moveDir;
 
         
                     //Debug.DrawRay(vertWorldPos, moveDir, Color.red);
                     //Debug.DrawLine(vertWorldPos, transform.position, Color.green);
                     ///Debug.Break();
                 }
 
             }
        
             meshfilters[j].mesh.vertices = verts;
             meshfilters[j].mesh.RecalculateBounds();
         }
     } 
 }



ERROR LINE FROM CODE:

 Vector3 colPointToMe = transform.position - collision.contacts[0].point;
 
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
2
Best Answer

Answer by LukaKotar · Nov 24, 2013 at 03:18 AM

If there is no collision, the array will be empty. 0 is always the first element in an array. If an array is empty, element 0 does not exist. Check if the array size is greater than 0 before using it.

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 BillyMFT · Mar 01, 2016 at 11:36 PM 1
Share

so how would OnCollisionEnter be called without a collision occurring and the collision object being passed to the function? I'm getting the same error occasionally.

Thanks

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

18 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

Related Questions

script car 0 Answers

Speedometer Error. PLS help ((( 0 Answers

Damage the Player 1 Answer

How to create a system of damage to a racing game 1 Answer

Car damage 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