-2

every help will be greatly appreciated. Im making a unity 3D game and my brain cant catch up with the problems that are poping up. I got 2 problems. First one is

Assets\Scripts\Timer.cs(30,11): error CS0120: An object reference is required for the non-static field, method, or property 'PlayerCarController.accelerationForce'

and the second

Assets\Scripts\Timer.cs(39,11): error CS0120: An object reference is required for the non-static field, method, or property 'PlayerCarController.accelerationForce'

I got 2 scripts first one is PlayerCarController

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

public class PlayerCarController : MonoBehaviour
{
    [Header("Wheels collider")]
    public WheelCollider frontLeftWheelCollider;
    public WheelCollider frontRightWheelCollider;
    public WheelCollider backLeftWheelCollider;
    public WheelCollider backRightWheelCollider;

    [Header("Wheels Transfrom")]
    public Transform frontLeftWheelTransfrom;
    public Transform frontRightWheelTransfrom;
    public Transform backLeftWheelTransfrom;
    public Transform backRightWheelTransfrom;

    [Header("Car Engine")]
    public float accelerationForce = 300f;
    public float breakingForce = 3000f;
    private float presentBreakForce = 0f;
    private float presentAcceleration = 0f;

    [Header("Car Steering")]
    public float wheelsTorque = 35f;
    private float presentTurnAngle = 0f;



    private void Update()
    {
        MoveCar();
        CarSteering();
        ApplyBreaks();
    }

    private void MoveCar()
    {
        //AWD
        frontLeftWheelCollider.motorTorque = presentAcceleration;
        frontRightWheelCollider.motorTorque = presentAcceleration;
        backLeftWheelCollider.motorTorque = presentAcceleration;
        backRightWheelCollider.motorTorque = presentAcceleration;

        presentAcceleration = accelerationForce * Input.GetAxis("Vertical");
     }

     private void CarSteering()
     {
          presentTurnAngle = wheelsTorque * Input.GetAxis("Horizontal");
          frontLeftWheelCollider.steerAngle = presentTurnAngle;
          frontRightWheelCollider.steerAngle = presentTurnAngle;

          SteeringWheels(frontLeftWheelCollider, frontLeftWheelTransfrom);
          SteeringWheels(frontRightWheelCollider, frontRightWheelTransfrom);
          SteeringWheels(backLeftWheelCollider, backLeftWheelTransfrom);
          SteeringWheels(backRightWheelCollider, backRightWheelTransfrom);
    }

    void SteeringWheels(WheelCollider WC, Transform WT)
    {

          Vector3 position;
          Quaternion rotation;


          WC.GetWorldPose(out position, out rotation);

          WT.position = position;
          WT.rotation = rotation;
      }

      public void ApplyBreaks()
      {
            if(Input.GetKey(KeyCode.Space))
                presentBreakForce = breakingForce;

            else
               presentBreakForce = 0f;


            frontLeftWheelCollider.brakeTorque = presentBreakForce;
            frontRightWheelCollider.brakeTorque = presentBreakForce;
            backLeftWheelCollider.brakeTorque = presentBreakForce;
            backRightWheelCollider.brakeTorque = presentBreakForce;
    }
}

and the second script (where i get the issues from) is Timer

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

public class Timer : MonoBehaviour
{
    [Header("Timer")]
    public float countDownTimer = 5f;

    [Header("Things to stop")]
    public PlayerCarController playerCarController;
    public OpponentCar OpponentCar;
    public OpponentCar OpponentCar1;
    public OpponentCar OpponentCar2;
    public OpponentCar OpponentCar3;
    public OpponentCar OpponentCar4;

    public Text countDownText;

    void Start()
    {
        StartCoroutine(TimeCount());
    }

    void Update()
    {
      if(countDownTimer > 1)
      {
          PlayerCarController.accelerationForce = 0f;
          OpponentCar.movingSpeed = 0f;
          OpponentCar1.movingSpeed = 0f;
          OpponentCar2.movingSpeed = 0f;
          OpponentCar3.movingSpeed = 0f;
          OpponentCar4.movingSpeed = 0f;
          }
          else if(countDownTimer == 0)
          {
          PlayerCarController.accelerationForce = 300f;
          OpponentCar.movingSpeed = 12f;
          OpponentCar1.movingSpeed = 13f;
          OpponentCar2.movingSpeed = 14f;
          OpponentCar3.movingSpeed = 9f;
          OpponentCar4.movingSpeed = 8f;



          }
      }


    IEnumerator TimeCount()
    {

        while(countDownTimer > 0)
        {
            countDownText.text = countDownTimer.ToString();
            yield return new WaitForSeconds(1f);
            countDownTimer--;
        }

        countDownText.text = "GO";
        yield return new WaitForSeconds(1f);
        countDownText.gameObject.SetActive(false);
      }
    }

Tried to reimport all and trying to find the solution by myself but i just dont understand

derHugo
  • 83,094
  • 9
  • 75
  • 115
Davinci
  • 1
  • 2
  • 1
    Does this answer your question? [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Absinthe Jun 02 '23 at 21:11
  • The above describes VB.NET but is common to all languages including C#/Unity. Basically you didn't hook something up in the inspector, or you didn't populate a variable. – Absinthe Jun 02 '23 at 21:14
  • You get the error because your variable playerCarController is not used but you referenced the class.. hence it says you need to reference an object... Casing is important – BugFinder Jun 02 '23 at 21:26

1 Answers1

0

public PlayerCarController playerCarController; You need

 playerCarController=new PlayerCarController();

before using PlayerCarController.accelerationForce = 0f;

or Make accelerationForce static

sorsa mele
  • 19
  • 4