Hey guys,
first time poster here, not sure if this is the right thread…
I’ve been following the survival shooter tutorial using my own assets instead of the ones providing, however i am using the same player movement script demonstrated.
Problem is, when i run the game i get the error “Null reference exception: Object reference not set to an instance of an object” and my character refuses to move!
![]()
Any help would be greatly appreciated.
Thanks in advance! ![]()
using UnityEngine;
using System.Collections;
public class PlayerMovement : MonoBehaviour {
public float speed = 6f;
Vector3 movement;
Rigidbody playerRigidbody;
int floorMask;
float camRaylength = 100f;
void awake()
{
floorMask = LayerMask.GetMask ("Floor");
playerRigidbody = GetComponent<Rigidbody> ();
}
void FixedUpdate()
{
float h = Input.GetAxisRaw("Horizontal");
float v = Input.GetAxisRaw("Vertical");
Move (h, v);
Turning ();
}
void Move (float h, float v)
{
movement.Set (h, 0f, v);
movement = movement.normalized * speed * Time.deltaTime;
playerRigidbody.MovePosition (transform.position + movement);
}
void Turning()
{
Ray camRay = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit floorHit;
if(Physics.Raycast (camRay, out floorHit, camRaylength, floorMask))
{
Vector3 playerToMouse = floorHit.point - transform.position;
playerToMouse.y = 0f;
Quaternion newRotation = Quaternion.LookRotation(playerToMouse);
playerRigidbody.MoveRotation (newRotation);
}
}
}
