Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Random Error when using Velocity.magnitude

Discussion in 'Physics' started by TGKG, Jan 29, 2015.

  1. TGKG


    Dec 31, 2014
    Hi there,
    I am trying to manipulate the velocity of an object after a collision with a parent/child object. Everything works fine for quite a while and then I randomly get the following rigidbody error:

    "MissingComponentException: There is no 'Rigidbody' attached to the "Child1" game object,
    but a script is trying to access it. You probably need to add a Rigidbody to the
    game object "Child1". Or your script needs to check if the component is attached before using it.
    UnityEngine.Rigidbody.get_velocity () (at C:/BuildAgent/work/d63.......60/artifacts/EditorGenerated/NewDynamics.cs:560)
    Player.OnCollisionEnter (UnityEngine.Collision collision) (at Assets/scripts/Player.cs:46)"

    I have a parent object with a rigidbody. This parent object has 2 child objects (without rigid bodies). A Ball object collides with one of the child of the parent. I use the following OnCollisionEnter script attached to the parent to determine the magnitude of the Ball velocity and which child object has been hit.

    Code (CSharp):
    2. void OnCollisionEnter(Collision collision)
    3. {
    4.     ContactPoint contact = collision.contacts[0];
    5. //error occurs at his point where Magnitude is being calculated
    6.     float Magnitude = contact.otherCollider.rigidbody.velocity.magnitude;
    7.     print("Velocity Magnitude: " + Magnitude);
    9.     if( == "Ball")
    10.     {
    11.         switch(
    12.         {          
    13.         case "Child1":
    14.             contact.otherCollider.rigidbody.velocity = new Vector3( -Magnitude * Mathf.Cos(60.0f * Mathf.Deg2Rad), Magnitude * Mathf.Sin(60.0f * Mathf.Deg2Rad), 0);
    15.             break;
    17.         case "Child2":
    18.             contact.otherCollider.rigidbody.velocity = new Vector3( -Magnitude * Mathf.Cos(60.0f * Mathf.Deg2Rad), Magnitude * Mathf.Sin(60.0f * Mathf.Deg2Rad), 0);
    19.             break;
    20.         }
    21.     }
    22. }
    I can't add a rigidbody to the child as this messes everything up.
    Why am I getting this error??
    Should I attach this script to the Ball object instead??

    I have tried so many things and cannot eliminate this error
  2. MatthewW


    Nov 30, 2006
    I'm assuming this is line 46:

    float Magnitude = contact.otherCollider.rigidbody.velocity.magnitude;

    This line happens on all collisions with all objects; it's outside of your if/switch statements. When the parent object collides with something that doesn't have a Rigidbody attached you're getting the exception.
  3. TGKG


    Dec 31, 2014
    Thank you MatthewW.
    Yes this is line 46, "float Magnitude ..." statement.
    The parent object has a rigidbody, the Ball has a rigidbody, however the children of the parent Do Not have a rigidbody.

    When the Ball collides with one of the child everything works (ie the collider script works) for some time as it should and then all of a sudden at some future collision this error appears.
    The script shown is attached to the parent object and in the script I am trying to adjust the velocity of the Ball. Is this good code or should this script be attached to the Ball?

    Actually all I am trying to do is adjust the direction vector of the ball after the collision, is there a better way to do this rather than using the cos() and sin() Math?