- Home /
Camera Ray - NullReferenceException: Object reference not set to an instance of an object
Hello I have a small problem. I have switched to Unity 2018.2.
My problem is I have character controller and I turn the player with mouse. Here is my code
public float movementSpeed;
public float walkingSpeed;
public float runningSpeed;
Rigidbody rgBody;
Camera viewCamera;
Vector3 moveAmount;
void Start ()
{
rgBody = GetComponent<Rigidbody>();
viewCamera = Camera.main;
}
// Update is called once per frame
void Update ()
{
Vector3 input = new Vector3(Input.GetAxisRaw("Horizontal"),0,Input.GetAxisRaw("Vertical"));
Vector3 inputDir = input.normalized;
bool running = Input.GetKey(KeyCode.LeftShift);
if (running)
{
movementSpeed = runningSpeed;
}
else
{
movementSpeed = walkingSpeed;
}
moveAmount = inputDir * movementSpeed;
Ray ray = viewCamera.ScreenPointToRay(Input.mousePosition);
Plane groundPlane = new Plane(Vector3.up, Vector3.zero);
float rayDistance;
if (groundPlane.Raycast(ray, out rayDistance))
{
Vector3 point = ray.GetPoint(rayDistance);
Debug.DrawLine(ray.origin,point,Color.red);
transform.LookAt(point);
}
}
private void FixedUpdate()
{
rgBody.MovePosition(rgBody.position + moveAmount * Time.fixedDeltaTime);
}
}
But when I start the game I get this error "NullReferenceException: Object reference not s et to an instance of an object Hrac_POHYB.Start () (at Assets/SKRIPTY/Hrac_POHYB.cs:24) "
On older versions of Unity this code works but in the 2018.2 Unity it doesn't work. Anybody knows what is wrong ?
Are you sure you have provided the Hrac_POHYB
script? There is absolutely no reason the Start
function in the provided script could throw a NullReferenceException
$$anonymous$$y whole script looks like this.It is also the only script in my project. But I have managed to fix that. I had to make viewCamera public and asign camera to it.
But it still doesn't make sense to me why do I get this error. I have even copied whole movement script from Sebastian Lague (link text and even that didn't work,even when I have edited it.(same eror)
This is how my whole script looks like.
using System.Collections; using System.Collections.Generic; using UnityEngine; [RequireComponent(typeof(Rigidbody))] public class Hrac_POHYB : $$anonymous$$onoBehaviour { public float movementSpeed; public float walkingSpeed; public float runningSpeed; Rigidbody rgBody; Camera viewCamera; Vector3 moveAmount; void Start () { rgBody = GetComponent<Rigidbody>(); viewCamera = Camera.main; } // Update is called once per frame void Update () { Vector3 input = new Vector3(Input.GetAxisRaw("Horizontal"),0,Input.GetAxisRaw("Vertical")); Vector3 inputDir = input.normalized; bool running = Input.Get$$anonymous$$ey($$anonymous$$eyCode.LeftShift); if (running) { movementSpeed = runningSpeed; } else { movementSpeed = walkingSpeed; } moveAmount = inputDir * movementSpeed; Ray ray = viewCamera.ScreenPointToRay(Input.mousePosition); Plane groundPlane = new Plane(Vector3.up, Vector3.zero); float rayDistance; if (groundPlane.Raycast(ray, out rayDistance)) { Vector3 point = ray.GetPoint(rayDistance); Debug.DrawLine(ray.origin,point,Color.red); transform.LookAt(point); } } private void FixedUpdate() { rgBody.$$anonymous$$ovePosition(rgBody.position + moveAmount * Time.fixedDeltaTime); } }
Have you tried to:
Close Unity
Delete the Library (and Temp) folders at the root of the project
Open Unity again, and let it reimport all your files
Your answer
Follow this Question
Related Questions
mouse position on terrain 1 Answer
Why is the camera.screenpointtoray off? 1 Answer
is it possible to have raycasts to cover the whole screen in one Update() time? 0 Answers
Raycast won't work after Camera transforms position. Please help! 1 Answer
Mouseclick GameObject through the view of a RenderTexture 0 Answers