First of all I am very new to C# (actually any coding) and am learning on the go for a project. Im trying to make a crosshair that turns red when you are close since I have limited the distance at which the object interaction works, I want a GUI change to tell you that you are close enough to use the object. All I get is the “NullReferenceException: Object reference not set to an instance of an object” error for the Crosshair script.
here is the mouseover script which checks distance to player from the object.
public class Mouseoverbool : MonoBehaviour
{
public bool mouseover;
private checkplayerdist playerDistScript;
void Start()
{
playerDistScript = GetComponent<checkplayerdist>();
}
void OnMouseOver()
{
if(playerDistScript.playerclose)
{
mouseover = true;
}
}
}
and the crosshair script thats causing the problem
ublic class CrosshairGUI : MonoBehaviour
{
private Color colour;
private Mouseoverbool islooking;
void OnGUI()
{
GUI.color = colour;
GUI.Label (new Rect(Screen.width/2,Screen.height/2, 50, 50), "X");
}
void Start()
{
islooking = GetComponent<Mouseoverbool>();
}
void Update()
{
if(islooking.mouseover)
{
colour = Color.red;
}
else
{
colour = Color.white;
}
}
}