Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

no 'Rigidbody2D' attached to the "PlayerBird(Clone)" ERROR

Discussion in '2D' started by GreenTee, Jan 1, 2015.

  1. GreenTee

    GreenTee

    Joined:
    Jul 25, 2014
    Posts:
    11
    Hello,

    i followed a tutorial to make my own "flappy bird(game on android) "clone but a script
    form the video gives me an error message(only as soons as i start the game):
    ----
    MissingComponentException: There is no 'Rigidbody2D' attached to the "PlayerBird(Clone)" game object, but a script is trying to access it.
    You probably need to add a Rigidbody2D to the game object "PlayerBird(Clone)". Or your script needs to check if the component is attached before using it.
    UnityEngine.Rigidbody2D.get_velocity () (at C:/BuildAgent/work/d63dfc6385190b60/artifacts/EditorGenerated/Physics2DBindings.cs:1194)
    GroundMovement.FixedUpdate () (at Assets/Scripts/GroundMovement.cs:20)

    ---

    The problem is there is no "PlayerBird(Clone)" and the GameObject "PlayerBird" has a Rigidbody2D.

    I found out that if i set the code in the "FixedUpdate()" as a command, the error disappears.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class GroundMovement : MonoBehaviour {
    5.     Rigidbody2D player;
    6.  
    7.     void Start () {
    8.         GameObject player_go = GameObject.FindGameObjectWithTag("Player");
    9.      
    10.         if(player_go == null) {
    11.             Debug.LogError("Couldn't find an object with tag 'Player'!");
    12.             return;
    13.         }
    14.      
    15.         player = player_go.rigidbody2D;
    16.      
    17.     }
    18.  
    19.     void FixedUpdate() {
    20.         float vel = player.velocity.x * 0.75f; // this code as comment repairs everything
    21.      
    22.         transform.position = transform.position + Vector3.right * vel * Time.deltaTime; // this code as comment repairs everything
    23.     }
    24. }
    25.  
    I don't know what to do because there is no "Clone" and the Rigidbody2D is attached to the normal GameObject.
     

    Attached Files:

    Last edited: Jan 1, 2015
  2. OboShape

    OboShape

    Joined:
    Feb 17, 2014
    Posts:
    836
    I havent worked through a tutorial for this, but ill try and give an idea if i can :)

    just noticed on your code
    player= player_go.rigidbody2D;

    see if changing this line to
    Code (CSharp):
    1.   player = player_go.GetComponent<rigidbody2D>();
     
    Last edited: Jan 2, 2015
    GreenTee likes this.
  3. GreenTee

    GreenTee

    Joined:
    Jul 25, 2014
    Posts:
    11
    nothing changes but this is only for a better "look". Later i will try an other parallax script :)

    Other Question: Do you know how to connect 2 scripts and their fuctions?

    In my UI script i have written down a code to count everytime the birds hits a collider, that adds a point to the counter, but in an other script i cant use the script functions. I am new in programming things :d

    Score script:_____________________________
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3. using System.Collections;
    4.  
    5. public class ScoreManager : MonoBehaviour
    6. {
    7.     public static int score = 0;        // The player's score.
    8.     Text text;                      // Reference to the Text component.
    9.  
    10.     void Awake ()
    11.     {
    12.         // Set up the reference.
    13.         text = GetComponent <Text> ();
    14.         // Reset the score.
    15.         score = 0;
    16.     }
    17.  
    18.     void Update ()
    19.     {
    20.         // Set the displayed text to be the word "Score" followed by the score value.
    21.         text.text = "Score: " + score;
    22.     }
    23.  
    24.     static public void AddPoint(){
    25.         score += 1 + score;
    26.  
    27.         }
    28. }
    29.  
    30.  
    ScoreAddingScript:__________________

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class ScoreAddingScript : MonoBehaviour {
    5.  
    6.     void OnTriggerEnter2D(Collider2D collider) {
    7.             if (collider.tag == "Player") {
    8.             Score.AddPoint(); // Error - I want to use the AddPoint function form the class "Score" to add a point if player collides with collider.
    9.             gameObject.SetActive(false);
    10.                 }
    11.         }
    12.  
    13.  
    14. }
    15.  
     
    Last edited: Jan 2, 2015
  4. OboShape

    OboShape

    Joined:
    Feb 17, 2014
    Posts:
    836
    you dont look like you have a Score Class.

    within your ScoreManager class you have declared a static int and a static function, and thats common to the class not the instance so, you should be able to say

    Code (CSharp):
    1. ScoreManager.AddPoint();
    on line 9 of your score adding script

    also your AddPoint() function
    Code (CSharp):
    1.     static public void AddPoint(){
    2.         score +=score;
    3.         }
    should work, or just score++; ;)

    best direction i can give at this time is to have a good look through the live lesson archives, and in particular
    http://unity3d.com/learn/tutorials/.../communicating-between-components-gameobjects
    to give a better understanding how to communicate between objects and scripts

    hope that helps.

    have you got a link to the tutorial please so i can have a look to get a better undersanding what your working through
     
    GreenTee likes this.
  5. OboShape

    OboShape

    Joined:
    Feb 17, 2014
    Posts:
    836
    as a note, when a gameobject has (clone) attached, i think it usually means its created on the fly, ie Instantiated, so check the prefab if there is one (cant see from your pic) if theres a RigidBody2D component on it
     
    aLeila91 and GreenTee like this.
  6. GreenTee

    GreenTee

    Joined:
    Jul 25, 2014
    Posts:
    11
    I really really really thank you!

    This is form youtube:
     
  7. Frrank

    Frrank

    Joined:
    Feb 9, 2015
    Posts:
    1
    1. player = player_go.GetComponentInParent<Rigidbody2D>;
    2. i think you need to add this instead of straight referencing your player