0

Error Message: UnassignedReferenceException: The variable groundCheckTransform of Player has not been assigned. You probably need to assign the groundCheckTransform variable of the Player script in the inspector. UnityEngine.Transform.get_position () (at :0) Player.FixedUpdate () (at Assets/Scripts/Player.cs:36)

image of inspector

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

public class Player : MonoBehaviour
{
     private bool jumpKeyPressed;
     private float horizontalMovement;
     private Rigidbody rigidcomponet;
     public int jforce = 7;
     public int movementspeed = 2;
     public Transform groundCheckTransform=null;

    // Start is called before the first frame update
    void Start()
    {
        rigidcomponet = GetComponent <Rigidbody>();
    }

    // Update is called once per frame
    void Update()
    {
        // Check if space clicked down
        if (Input.GetKeyDown(KeyCode.Space))
        {
           jumpKeyPressed = true;
        }

        horizontalMovement = Input.GetAxis("Horizontal");
    }
// Updates every physics update. Aka 100 times per second
    void FixedUpdate()
    {
         rigidcomponet.velocity = new Vector3(0,rigidcomponet.velocity.y,horizontalMovement*movementspeed);

        if (Physics.OverlapSphere(groundCheckTransform.position, 0.1f).Length == 0)
        {
            return;
        }

        if (jumpKeyPressed)
        {
           rigidcomponet.AddForce(Vector3.up * jforce, ForceMode.VelocityChange);
           jumpKeyPressed=false;
        }


    }

}
General Grievance
  • 4,555
  • 31
  • 31
  • 45
Jason Lin
  • 1
  • 2

2 Answers2

0

Plus I suggest you for Rigidbody to be called in Awake() like this:

void Awake()
{

    rigidcomponet = GetComponent <Rigidbody>();

}
Unity_cube
  • 47
  • 8
-1

Hmmm... Try this :

public Transform groundCheckTransform;

Unity_cube
  • 47
  • 8