Search Unity

Missing Component: Rigidbody. When there is a rigidbody component attached.

Discussion in 'Editor & General Support' started by Tom212, Aug 18, 2015.

  1. Tom212

    Tom212

    Joined:
    May 24, 2015
    Posts:
    27
    Hi,

    I'm currently trying to complete the survival shooter unity tutorial.

    I've hit a strange wall, my code is exactly as the complete code is shown:

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3.  
    4. public class PlayerMovement : MonoBehaviour
    5. {
    6.     public float speed = 6f;
    7.  
    8.     Vector3 movement;
    9.     Animator anim;
    10.     Rigidbody playerRigidbody;
    11.     int floorMask;
    12.     float camRayLength = 100f;
    13.  
    14.     void Awake () {
    15.  
    16.         floorMask = LayerMask.GetMask ("Floor");
    17.         anim = GetComponent<Animator> ();
    18.         playerRigidbody = GetComponent <Rigidbody> ();
    19.         Debug.Log ("awake");
    20.  
    21.     }
    22.  
    23.     void Start () {
    24.  
    25.     }
    26.  
    27.     void FixedUpdate () {
    28.  
    29.         float h = Input.GetAxisRaw("Horizontal");
    30.         float v = Input.GetAxisRaw("Vertical");
    31.  
    32.         Debug.Log (h);
    33.         Debug.Log (v);
    34.         Debug.Log ("update");
    35.         Move (h, v);
    36.         Turning ();
    37.         Animating (h, v);
    38.  
    39.     }
    40.  
    41.     void Move (float h, float v) {
    42.    
    43.         movement.Set (h, 0f, v);
    44.  
    45.         movement = movement.normalized * speed * Time.deltaTime;
    46.  
    47.         playerRigidbody.MovePosition (transform.position + movement);
    48.  
    49.  
    50.     }
    51.  
    52.     void Turning () {
    53.  
    54.         Ray camRay = Camera.main.ScreenPointToRay (Input.mousePosition);
    55.         RaycastHit floorHit;
    56.  
    57.         if (Physics.Raycast (camRay, out floorHit, camRayLength, floorMask)) {
    58.             Vector3 playerToMouse = floorHit.point - transform.position;
    59.             playerToMouse.y = 0f;
    60.  
    61.             Quaternion newRotation = Quaternion.LookRotation (playerToMouse);
    62.             playerRigidbody.MoveRotation (newRotation);
    63.         }
    64.  
    65.     }
    66.  
    67.     void Animating (float h, float v) {
    68.         bool walking = h != 0f || v != 0f;
    69.         anim.SetBool ("IsWalking", walking);
    70.     }
    71.  
    72. }
    73.  
    74.  
    However when it runs I get this error:

    Even though as you can see there is in fact a rigidbody component on the GameObject the script is attached to.



    Any ideas what could be causing this?
     
  2. darkhog

    darkhog

    Joined:
    Dec 4, 2012
    Posts:
    2,218
    Try this.gameObject.GetComponent<RigidBody>() - similar stuff was happening to me as well until I've started to write this like that.
     
  3. Tom212

    Tom212

    Joined:
    May 24, 2015
    Posts:
    27
    Didn't change anything sadly, same error message. It appears adamant that there is no rigid body attached to the object. Could there be something wrong with the rigidbody component?
     
  4. Tom212

    Tom212

    Joined:
    May 24, 2015
    Posts:
    27
    I don't know why but I had to delete the player gameobject and just re-create it. It really didn't like it for some reason, all seems fixed now.
     
  5. Nilmag

    Nilmag

    Joined:
    Feb 6, 2014
    Posts:
    1
    I think i know what you did as i just did the same thing, you probably added the PlayerMovement script to the player 3d model child object whereas it needs to be attached to the parent object, where the rigid body is stored.
     
    Sab_Rango and Lolipopgi like this.
  6. Lolipopgi

    Lolipopgi

    Joined:
    Dec 10, 2016
    Posts:
    1
    I had the same problem with my asteroids. The model "prop_asteroid_01" also had "Mover" script as a component, but since my rigidbody component was on the parent "Asteroid" (which also had the script "Mover"), I only had to delete the script from the child and it didn't give me the error anymore.