- Home /
Can you change GUI Crosshair colour with OnMouseOver rather than Raycasting
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;
}
}
}
Your question is a little different from the title.
Using raycasting allows you to do so much more than just check if the object behind the cursor.
You can tell distance better for one.
Answer by Ben-Stoneman · Jun 20, 2013 at 03:50 PM
Some references need to be public and the object holding the script needs to be assigned in the inspector
public checkplayerdist playerDistScript;
public Mouseoverbool islooking;
You reference the scripts, however they are not found.
OnMouseOver is very limited with the functions you seem to be going for. Raycasting allows alot more.
$$anonymous$$ight be the case as Im finding this near Impossible. I'll look into raycasting I think, thanks for your time reading this anyway.