- Home /
Question by
Junske · Dec 19, 2014 at 09:41 PM ·
javascriptraycast
OnGUI nullreference
The script is working, however i'm still receiving "hits collider is null". i Just want to get rid of the null it's very annoying. I know something is wrong with the way OnGUI is comparing. But i don't know what i'm doing wrong.
function Update () {
if (Physics.Raycast (transform.position, transform.forward, hit, 100) && (hit.collider.gameObject.tag == "kat"))
{
Print("I am looking at" + hit.transform.tag);
}
else if (Physics.Raycast (transform.position, transform.forward, hit, 100)&&(hit.collider.gameObject.tag == "boek"))
{
Print("I am looking at" + hit.transform.tag);
}
else if (Physics.Raycast (transform.position, transform.forward, hit, 100) && (hit.collider.gameObject.tag == "pen"))
{
Print("I am looking at" + hit.transform.tag);
}
else
{
Print("I am looking at nothing");
}
}
line 4 is probably causing the null
function OnGUI(){
if(hit == null) Debug.Log("hit is null");
if(hit.collider == null) Debug.Log("hit's collider is null");
if(hit.collider && hit.collider.gameObject.tag == "kat")
{
GUI.DrawTexture(position, katImage); //"draws" image
}
else if(hit.collider && hit.collider.gameObject.tag == "boek")
{
GUI.DrawTexture(position, boekImage); //"draws" image
}
else if(hit.collider && hit.collider.gameObject.tag == "pen")
{
GUI.DrawTexture(position, penImage); //"draws" image
}
else
{
return false;
}
}
Comment