CS0246 RigidBody2D new to game development need help!,RigidBody2D error: CS0246

I am making an endless runner using a tutorial and every time it gives me error CS0246 for RigidBody2D even though I added to RigidBody 2D component.

Here is my code:

using System.Collections; 
using System.Collections.Generic; 
using UnityEngine;

public class BackroundScroller : MonoBehaviour {   
	public Boxcollider2D collider;   
	public RigidBody2D rb;  
	private float width;   
	private float scrollspeed = -2f;

    // Start is called before the first frame update
    void Start()
    {
        collider = GetComponent();
        rb = GetComponent();
        width = collider.size.x;
        collider.enabled = false;
        rb.velocity = new Vector2(scrollspeed, 0);
    } 

    // Update is called once per frame
    void Update ()
    {
        if (transform.position.x < -width);
        {
           Vector2 resetPosition = new Vector2(width * 2f, 0);
           transform.position = (Vector2)transform.position + resetPosition;
        }
    } 
}

Here is the video link:

,

Just remember what is happening in Update. That code is running every time the screen refreshes, many times every second, so basically you are moving too fast (2 units every frame).

There is another thing though and that is that the time between each screen refresh varies. It’s normal to multiple speed by Time.DeltaTime, which is that amount of time since the last refresh. On slow computers, Time.DeltaTime will be larger so bigger moves are made but not so often. On fast systems, it’s smaller so smaller moves are made but more frequently. That way the distance covered each second is the same on all computers.

So, multiply resetPosition by Time.DeltaTime and that should slow it down for you. You may want to change the basic speed (you are using 2F) because the game may now be too slow.

If I may suggest, you need to take some more tutorials. This has been a very long thread over basic errors. There is an expectation in the forum that you do some work yourself. Try going to Learn.Unity.Com and doing the Unity Essentials course there. All this will make a lot more sense after that.

I’m afraid this code needs a lot of work. You really need to go back and rewatch the video. For example, the syntax for the collider and rb are wrong. Check the video for how they should be. e.g. collider = GetComponent();

I couldn’t find any problems with my code. It looks identical to the one in the video. could you specify what is wrong with it?

You can’t just use GetCommponent, you have to tell it what kind of component to get like this: rb = GetComponent();

The answer is quite simple. You are not spelling the classes correctly.

Rigidbody2D and BoxCollider2D

In line 26 you should multiply by Time.DeltaTime. When you calculate a distance to move, that distance is affected by the amount of time since the last game was drawn.

“RigidBody2D” is not a namespace, the namespace you are trying to use is called “Rigidbody2D” Simply rename every “RigidBody2D” to "“Rigidbody2D” without a capital “D.”