Says that variable has not been assigned(but I already did).

When I press play it gives me the error that the backWheel and frontWheel of CarController have not been assigned. I can’t figure out why because I made 2 wheels as the child of my bike then I put Rigidbody 2ds on them with circle 2d colliders. The bike has a rb 2d with a polygon collider 2d and 2 wheel joint 2ds. The wheel joints both have the connected rigid body set as the front and the back wheel. Plus my CarController script also has the wheel joints set for back and front including the rigid body for the bike under the rb variable. I have no clue why this is occurring please help me.

public float speed = 1500;
public float rotationSpeed = 15f;

private float movement = 0f;
private float rotation = 0f;

public WheelJoint2D backWheel;
public WheelJoint2D frontWheel;

public Rigidbody2D rb;

void Update()
{
   movement = -Input.GetAxisRaw("Vertical") * speed;
    Input.GetAxisRaw("Horizontal");
}

void FixedUpdate()
{
    if (movement == 0f)
    {
        backWheel.useMotor = false;
        frontWheel.useMotor = false;
    }
    else
    {
        backWheel.useMotor = true;
        frontWheel.useMotor = true;
        JointMotor2D motor = new JointMotor2D { motorSpeed = movement, maxMotorTorque = 10000 };
        backWheel.motor = motor;
        frontWheel.motor = motor;
    }

    rb.AddTorque(-rotation * rotationSpeed * Time.fixedDeltaTime);  
}

}

Did you remember to assign them in the inspector?

You need to drag and drop the GameObject that has the RigidBody2D component attached to it in the Inspector window. If this car is a Prefab, you should also be sure to press the Apply button, or it will only affect the instance in the scene. Anything shown in bold in the Inspector is unique to that instance, and isn’t going to be copied across your game.
Example


Edit 1:

I think I see one possibility. You have two wheel joints, but they are both attached to the same GameObject. In order to build references to these two, you have to use GetComponents() inside there. Try this, instead:

using UnityEngine.Assertions; // This helps us track bugs easier

class CarController
{
  [SerializeField] private GameObject m_wheelJointObject;

  // We're using auto properties, so as not to allow other scripts to modify them on accident
  public WheelJoint2D backWheel {get; private set;}
  public WheelJoint2D frontWheel {get; private set;}

  private void Awake( )
  {
    Assert.IsNotNull( m_wheelJointObject, "There is no object with wheel joints." );

    var wheelJoints = m_wheelJointObject.GetComponents<WheelJoint2D>();

    backWheel = wheelJoints[0]; // You may need to swap these. It depends on the order in the Inspector. Use the cog icon to move components up or down if you want to swap them there.
    frontWheel = wheelJoints[1];
  }
}

Disclaimer: I didn’t actually run this code, so I hope there are no typos!


Edit 2: So you’ve assigned that GameObject, as I can see in your screenshot. But that Assert.IsNotNull I slipped in there is telling us quite plainly that it’s not set. This leads me to guess that there’s another CarController in your scene that you’re not setting up. Is there another GameObject you added and maybe forgot about? Or perhaps you added a CarController to something on complete accident? To find out which object is logging these errors, add this line to Awake():

private void Awake( )
{
  // Add this line to the top!
  Assert.IsNotNull( m_wheelJointObject, "The following object is not fully set up: " + name );

  // ... more existing code
}

I hope this helps track it down! Good luck!