Hello i am having problems moving an object using add force in unity2d
The Error im getting is:
NullReferenceException: Object reference not set to an instance of an object
Walk.Update () (at Assets/CharacterScripts/Movement/Walk.cs:18)
And this is my code so far:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Walk : MonoBehaviour
{
public Rigidbody2D rb2D;
public float force = 5;
private void Start() {
rb2D = gameObject.AddComponent<Rigidbody2D>();
}
void Update()
{
if (Input.GetKeyDown("d"))
{
rb2D.AddForce(new Vector2(0f, force), ForceMode2D.Impulse);
}
}
}