Hi,
I’m new to coding and have had this error pop up after trying to enter play mode. Can you please help:
NullReferenceException: Object reference not set to an instance of an object
Player.Move () (at Assets/Scripts/Player.cs:41)
Player.Update () (at Assets/Scripts/Player.cs:29)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Player : MonoBehaviour
{
[Header (“Movement”)]
public float moveSpeed;
public float jumpForce;
[Header ("Camera")]
public float lookSensitivity;
public float maxLookX;
public float minLookX;
private float rotX;
private Camera cam;
private Rigidbody rig;
void awake ()
{
// get the components
cam = Camera.main;
rig = GetComponent<Rigidbody>();
}
void Update ()
{
Move();
CamLook();
}
// move horizontally based on movement inputs
void Move ()
{
// get the × and z inputs
float x = Input.GetAxis("Horizontal") * moveSpeed;
float z = Input.GetAxis("Vertical") * moveSpeed;
// apply the velocity
rig.velocity = new Vector3(x, rig.velocity.y, z);
}
void CamLook ()
{
float y = Input.GetAxis("Mouse X") * lookSensitivity;
rotX += Input.GetAxis("Mouse Y") * lookSensitivity;
rotX = Mathf.Clamp(rotX, minLookX, maxLookX);
cam.transform.localRotation = Quaternion.Euler(rotX, 0, 0);
transform.eulerAngles += Vector3.up * y;
}
}