There is no 'Rigidbody2D' attached to the 'blue' game object, but a script is trying to access it.

Im in C# beginner and i want help please.

MissingComponentException:
There is no ‘Rigidbody2D’ attached to the ‘blue’ game object, but a script is trying to access it.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class mo ve_script : MonoBehaviour {
    public KeyCode moveUp;
    public KeyCode moveDown;
    public KeyCode moveLeft;
    public KeyCode moveRight;

    // Use this for initialization
    void Start () {
      
    }
  
    // Update is called once per frame
    void Update () {
        if (Input.GetKeyDown(moveUp))
            GetComponent<Rigidbody2D>().velocity = new Vector3(0, 3, 0);

        if (Input.GetKeyDown(moveDown))
            GetComponent<Rigidbody2D>().velocity = new Vector3(0, -3, 0);

        if (Input.GetKeyDown(moveLeft))
            GetComponent<Rigidbody2D>().velocity = new Vector3(-3, 0, 0);

        if (Input.GetKeyDown(moveRight))
            GetComponent<Rigidbody2D>().velocity = new Vector3(3, 0, 0);


    }
}
[CODE/]

GetComponent tells the script to look at the same gameobject and find a rigidbody2d component. If there is none, it will return null. So whatever object your ve_script is attached to also needs a rigidbody2d component.

Also, use code tags. Using code tags properly - Unity Engine - Unity Discussions

First off, when you get a chance, please read over this post: Using code tags properly - Unity Engine - Unity Discussions

It tells you how to paste code using CODE tags, which formats your code nicely and makes it easier for everyone to read.

Anyways, the error is quite simple… game objects have “things” attached to them called components. A rigidbody is a component. A script is a component. A light is a component. There are lots of different components. You’re using a function called GetComponent which will look at the object your script is attached to and find the Rigidbody2D component on it. You haven’t added a Rigidbody2D component to it, as the error describes.

Now that all said, I think it’s painfully clear that you’re very green when it comes to Unity. There is nothing wrong with that, but at the sake of preventing you from making dozens upon dozens of posts asking for help, I’m going to point you towards doing some tutorials, as you really need to learn the basics. The forum is a great place to get help when you’re stuck, but it’s a terrible place to learn Unity, as no one is going to have time to walk you through everything you need to learn.

Take a look here to get started in Unity:
https://unity3d.com/learn/tutorials

Basically what you’re doing right now is jumping in the deep ocean and asking people how you swim :slight_smile: It’s perfectly fine that you’re new and don’t know how Unity works (we all have to start somewhere), but do yourself a favor and check out the tutorials.

Have fun!

2 Likes

Thanks ! I didn’t know where to learn C#. Thanks for tip ! :slight_smile:

Knowing a little bit of C# helps, but the tutorials will definitely walk you through all the basics. Are you completely new to programming? Or just new to C#? If it’s just that you’re new to C#, it shouldn’t be too hard to understand the syntax with some of these tutorials. If you’re new to programming, completely, it might be a good idea to check out some other articles on the internet to teach you programming fundamentals. But you can get by enough just by doing some of the tutorials that are out there.

One thing I’ll caution is that some of the tutorials on the Unity tutorials site (which I linked) are a bit out of date and might have some errors. I’d stick to the ones that are based on Unity 5. It will usually tell you what version the tutorial was made in.

I’ve personally done the Space Shooter, Roll-a-ball and Survival Shooter tutorials. The Survival had some issues with 5 though, I think, if I recall. But they do have an updated project for download for Unity 5.

The adventure game tutorial is fairly new, but that one has a bit more advanced of concepts, so I wouldn’t start with that one. I haven’t done the 2D UFO tutorial, but I think that’s newer and a good beginner one too.