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

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.

using UnityEngine;
using System.Collections;

public class GroundMovement : MonoBehaviour {
    Rigidbody2D player;
 
    void Start () {
        GameObject player_go = GameObject.FindGameObjectWithTag("Player");
     
        if(player_go == null) {
            Debug.LogError("Couldn't find an object with tag 'Player'!");
            return;
        }
     
        player = player_go.rigidbody2D;
     
    }
 
    void FixedUpdate() {
        float vel = player.velocity.x * 0.75f; // this code as comment repairs everything
     
        transform.position = transform.position + Vector3.right * vel * Time.deltaTime; // this code as comment repairs everything
    }
}

I don’t know what to do because there is no “Clone” and the Rigidbody2D is attached to the normal GameObject.

I havent worked through a tutorial for this, but ill try and give an idea if i can :slight_smile:

just noticed on your code
player= player_go.rigidbody2D;

see if changing this line to

  player = player_go.GetComponent<rigidbody2D>();
1 Like

nothing changes but this is only for a better “look”. Later i will try an other parallax script :slight_smile:

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:_______________________________

using UnityEngine;
using UnityEngine.UI;
using System.Collections;

public class ScoreManager : MonoBehaviour
{
    public static int score = 0;        // The player's score.
    Text text;                      // Reference to the Text component.
  
    void Awake ()
    {
        // Set up the reference.
        text = GetComponent <Text> ();
        // Reset the score.
        score = 0;
    }

    void Update ()
    {
        // Set the displayed text to be the word "Score" followed by the score value.
        text.text = "Score: " + score;
    }

    static public void AddPoint(){
        score += 1 + score;

        }
}

ScoreAddingScript:__________________

using UnityEngine;
using System.Collections;

public class ScoreAddingScript : MonoBehaviour {

    void OnTriggerEnter2D(Collider2D collider) {
            if (collider.tag == "Player") {
            Score.AddPoint(); // Error - I want to use the AddPoint function form the class "Score" to add a point if player collides with collider.
            gameObject.SetActive(false);
                }
        }


}

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

ScoreManager.AddPoint();

on line 9 of your score adding script

also your AddPoint() function

    static public void AddPoint(){
        score +=score;
        }

should work, or just score++; :wink:

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/modules/beginner/live-training-archive/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

1 Like

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

2 Likes

I really really really thank you!

This is form youtube:

  • player = player_go.GetComponentInParent;

  • i think you need to add this instead of straight referencing your player