Search Unity

Problem with rigidbody2d missing component exception

Discussion in 'Getting Started' started by gopixus87_unity, Sep 23, 2017.

  1. gopixus87_unity

    gopixus87_unity

    Joined:
    Sep 12, 2017
    Posts:
    3
    Hello. I'am making 2d game with tutorial (https://gamedevelopment.tutsplus.co...th-unity-player-and-ball-mechanics--cms-20860) based on unity 4 so there is a problem because i'am using 2017. I found som info in doc https://docs.unity3d.com/ScriptReference/Rigidbody-isKinematic.html . I replace orginal code rigidbody2D.isKinematic to new one but my project don't see rigidbody component.
    "MissingComponentException: There is no 'Rigidbody' attached to the "Ball" game object, but a script is trying to access it."
    Can you help me please? There is a code:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4.  
    5. public class BallScript : MonoBehaviour
    6. {
    7.  
    8.     private bool ballIsActive;
    9.     private Vector3 ballPosition;
    10.     private Vector2 ballInitialForce;
    11.     private Rigidbody rb;
    12.     public GameObject playerObject;
    13.  
    14.     void Start()
    15.     {
    16.         ballInitialForce = new Vector2(100.0f, 300.0f);
    17.         ballIsActive = false;
    18.         ballPosition = transform.position;
    19.     }
    20.  
    21.     void Update()
    22.     {
    23.         rb = GetComponent<Rigidbody>();
    24.      
    25.         if (Input.GetButtonDown("Jump") == true)
    26.         {
    27.          
    28.             if (!ballIsActive)
    29.             {
    30.            
    31.                 rb.isKinematic = false;
    32.  
    33.              
    34.                 rb.AddForce(ballInitialForce);
    35.  
    36.                
    37.                 ballIsActive = !ballIsActive;
    38.             }
    39.         }
    40.  
    41.         if (!ballIsActive && playerObject != null)
    42.         {
    43.  
    44.             // get and use the player position
    45.             ballPosition.x = playerObject.transform.position.x;
    46.  
    47.             // apply player X position to the ball
    48.             transform.position = ballPosition;
    49.         }
    50.  
    51.         // Check if ball falls
    52.         if (ballIsActive && transform.position.y < -6)
    53.         {
    54.             ballIsActive = !ballIsActive;
    55.             ballPosition.x = playerObject.transform.position.x;
    56.             ballPosition.y = -4.2f;
    57.             transform.position = ballPosition;
    58.  
    59.             rb.isKinematic = true;
    60.         }
    61.  
    62.     }
    63. }
     
    Last edited: Sep 23, 2017
    Enzzo likes this.
  2. Enzzo

    Enzzo

    Joined:
    Sep 9, 2017
    Posts:
    8
  3. Enzzo

    Enzzo

    Joined:
    Sep 9, 2017
    Posts:
    8
    Try this solution.
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4.  
    5. [RequireComponent(typeof(Rigidbody2D))]
    6. public class BallScript : MonoBehaviour{
    7.     private bool ballIsActive;
    8.     private Vector3 ballPosition;
    9.     private Vector2 ballInitialForce;
    10.     private Rigidbody2D rb;
    11.     public GameObject playerObject;
    12.  
    13.     void Start(){
    14.         ballInitialForce = new Vector2(100.0f, 300.0f);
    15.         ballIsActive = false;
    16.         ballPosition = transform.position;
    17.         rb = GetComponent<Rigidbody2D>();
    18.     }
    19.  
    20.     void Update(){
    21.  
    22.         if (Input.GetButtonDown("Jump") == true){
    23.  
    24.             if (!ballIsActive && rb != null) {
    25.    
    26.                 rb.isKinematic = false;
    27.      
    28.                 rb.AddForce(ballInitialForce);
    29.        
    30.                 ballIsActive = !ballIsActive;
    31.             }
    32.         }
    33.         if (!ballIsActive && playerObject != null){
    34.             // get and use the player position
    35.             ballPosition.x = playerObject.transform.position.x;
    36.             // apply player X position to the ball
    37.             transform.position = ballPosition;
    38.         }
    39.         // Check if ball falls
    40.         if (ballIsActive && transform.position.y < -6){
    41.             ballIsActive = !ballIsActive;
    42.             ballPosition.x = playerObject.transform.position.x;
    43.             ballPosition.y = -4.2f;
    44.             transform.position = ballPosition;
    45.             rb.isKinematic = true;
    46.         }
    47.     }
    48. }
    Cheers
     
    Last edited: Sep 26, 2017