- Home /
What does Object reference not set to an instance of an object mean?
Hellp I am new to C# and I have searched everything and I can not find the answer hope you can help.
using System.Collections; using System.Collections.Generic; using UnityEngine;
[RequireComponent(typeof(PlayerMotor))]
public class PlayerController : MonoBehaviour
{
public LayerMask movementmask;
Camera cam;
PlayerMotor motor;
RaycastHit hit;
// Use this for initialization
void Start()
{
cam = Camera.main;
motor = GetComponent<PlayerMotor>();
}
// Update is called once per frame
void Update()
{
if (Input.GetMouseButtonDown(0))
{
Ray ray = cam.ScreenPointToRay(pos: Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit, 100, movementmask))
{
motor.MovetoPoint(hit.point);
// stop focusing any objects
}
}
}
}
,using System.Collections; using System.Collections.Generic; using UnityEngine;
[RequireComponent(typeof(PlayerMotor))] public class PlayerController : MonoBehaviour { public LayerMask movementmask;
Camera cam;
PlayerMotor motor;
RaycastHit hit;
// Use this for initialization
void Start()
{
cam = Camera.main;
motor = GetComponent<PlayerMotor>();
}
// Update is called once per frame
void Update()
{
if (Input.GetMouseButtonDown(0))
{
Ray ray = cam.ScreenPointToRay(pos: Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit, 100, movementmask))
{
motor.MovetoPoint(hit.point);
// stop focusing any objects
}
}
}
}
Please post the full error message with the line number included.
Answer by Darkforge317 · May 16, 2018 at 01:26 AM
Copy and paste the entire error message so we can see what line of code is causing the problem.
The answer is: It means that one of your variables is null and you're trying to do something with it. If a variable is null, you can't do anything to its data since there is no data...
Answer by OscL1 · Jul 15, 2020 at 08:44 PM
First you did not show the full error so I can't give you any answer, but this error basically means that you have a variable that is been used in code but hasn't been assign to an instance. Double click on the error and it'll located the error for you.
Your answer
![](https://koobas.hobune.stream/wayback/20220612162745im_/https://answers.unity.com/themes/thub/images/avi.jpg)