- Home /
Using raycast to "collide" with tagged objects & enable Halo Component
I get the null reference exception for the hit.transform.gameObject.tag:
using UnityEngine;
using System.Collections;
public class SelectObject : MonoBehaviour {
RaycastHit hit;
GameObject gameObject1;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
Ray ray = camera.ScreenPointToRay(Input.mousePosition);
Debug.DrawRay(ray.origin, ray.direction * 100, Color.green);
if(Physics.Raycast(ray,100))
{
print ("Here is an object!");
if (hit.transform.gameObject.tag == ("Ground")){
Debug.DrawRay(ray.origin, ray.direction * 100, Color.red);
print ("You Hit the Ground");
}
else if (hit.transform.gameObject.tag == ("Player")){
Debug.DrawRay(ray.origin, ray.direction * 100, Color.blue);
gameObject.GetComponent<Halo>().enabled = true;
print ("You touched the Players object");
}
else{
print ("At least you hit something");
}
}
}
}
What am I missing here? Also I wanted to enable a component(Halo - by default it is disabled) of the Player's object when the raycast collides with it(Indicating I could select the object when the raycast collides with the tagged object by highlighting it with a halo)
You get a nullreferenceException because you forgot to use hit inside of the raycast method so ins$$anonymous$$d of
if(Physics.Raycast(ray,100))
it should be
if(Physics.Raycast(ray, hit,100))
Answer by Tatsunomi · Jun 02, 2013 at 10:57 PM
Thanks to -> Bigbat and ExtheSea for pointing out what I was missing for the raycasting. Turns out I missed putting a "out hit" on physic.raycast on line 17. I figured that some people can't seem to use the halo component so its better to use a component which does a similar effect which is the light component with the draw halo checkbox -> checked. This is what I did to enable the highlight when clicked.
using UnityEngine;
using System.Collections;
public class SelectObject : MonoBehaviour {
RaycastHit hit;
public Light myLight;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
Ray ray = camera.ScreenPointToRay(Input.mousePosition);
Debug.DrawRay(ray.origin, ray.direction * 100, Color.green);
if(Physics.Raycast(ray, out hit, 100))
{
print ("Here is an object!");
if (hit.transform.gameObject.tag == ("Ground")){
Debug.DrawRay(ray.origin, ray.direction * 100, Color.red);
print ("You Hit the Ground");
}
else if (hit.transform.gameObject.tag == ("Player")){
Debug.DrawRay(ray.origin, ray.direction * 100, Color.blue);
if ((Input.GetMouseButtonDown(0))&&(hit.transform.gameObject.tag == "Player")){
print("You Clicked the Player!");
GameObject light = GameObject.FindWithTag("Player");
myLight = light.GetComponent<Light>();
myLight.enabled = true;
}
else{
//ObjLight.light.enabled = false;
}
//gameObject.GetComponent<"Halo">().enabled = true;
print ("You touched the Players object");
}
else{
print ("At least you hit something");
}
}
}
}
So Basically I "get" the component of a gameObject tagged Player and enable it:
GameObject light = GameObject.FindWithTag("Player");
myLight = light.GetComponent<Light>();
myLight.enabled = true;
Answer by bigbat · Jun 02, 2013 at 02:44 PM
You must change line 17 to this:
if(physic.raycast(ray,out hit,100))
Thanks for showing me what I forgot to write sir but I also need to enable the Halo component of the gameObject but I don't know how to apply it correctly.
you have to do hit.transform.gameobject.GetComponent in line 26
I didn't use the Halo anymore BUT I used an alternative one with Halo also which is the Light Component. $$anonymous$$y only problem now is that the light component isn't enabled when clicked.
you could use a script like this:
using UnityEngine;
using System.Collections;
public class SelectObject : $$anonymous$$onoBehaviour {
RaycastHit hit;
//GameObject gameObject1; I think you dont need this
private bool highlithed; //new var for check whethere current hited object highlighted or not
private color[] oldmeshvrtcol;
// Use this for initialization
void Start () {
highlighted=false;
backupmesh();
}
// Update is called once per frame
void Update () {
Ray ray = camera.ScreenPointToRay(Input.mousePosition);
Debug.DrawRay(ray.origin, ray.direction * 100, Color.green);
if(Physics.Raycast(ray,out hit,100))
{
print ("Here is an object!");
if (hit.transform.gameObject.tag == ("Ground")){
Debug.DrawRay(ray.origin, ray.direction * 100, Color.red);
highlighted=false;
print ("You Hit the Ground");
}
else if (hit.transform.gameObject.tag == ("Player")){
Debug.DrawRay(ray.origin, ray.direction * 100, Color.blue);
highlighted=true;
print ("You touched the Players object");
}
else{
print ("At least you hit something");
highlighted=false;
}
}
if(highlighted)
highlightmesh();
else
resetmesh();
}
void backupmesh()
{
$$anonymous$$eshFilter meshFilter = gameObject.GetComponent<$$anonymous$$eshFilter>();
$$anonymous$$esh mesh = meshFilter.shared$$anonymous$$esh;
oldmeshvrtcol= mesh.colors;
}
void highlightmesh()
{
$$anonymous$$eshFilter meshFilter = gameObject.GetComponent<$$anonymous$$eshFilter>();
$$anonymous$$esh mesh = meshFilter.shared$$anonymous$$esh;
Color[] vertexColors = mesh.colors;
for(int i = 0; i < (int)(vertexColors.Length / 2); i++)
{
vertexColors[i] = Color.blue;//or any color you want
}
mesh.colors = vertexColors; // update the vertex colors
}
void resetmesh()
{
$$anonymous$$eshFilter meshFilter = gameObject.GetComponent<$$anonymous$$eshFilter>();
$$anonymous$$esh mesh = meshFilter.shared$$anonymous$$esh;
mesh.colors=oldmeshvrtcol;
}
}
it is just a modified version of your script with some changes.i hope this be helpful.
Your answer
Follow this Question
Related Questions
enable/disable specific components 3 Answers
Sharing components amongst different gameobjects 1 Answer
How to basically use GUI.Toggle? 2 Answers
enable and disable boxcollider (whats wrong with my script?) 2 Answers
Game Objects enable problems 0 Answers