Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Tatsunomi · Jun 02, 2013 at 02:35 PM · gameobjectcomponentenablenull reference exceptionhalo

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)

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ExTheSea · Jun 02, 2013 at 02:45 PM 0
Share

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))

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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;
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by bigbat · Jun 02, 2013 at 02:44 PM

You must change line 17 to this:

 if(physic.raycast(ray,out hit,100))
Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Tatsunomi · Jun 02, 2013 at 03:14 PM 0
Share

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.

avatar image ExTheSea · Jun 02, 2013 at 03:16 PM 0
Share

you have to do hit.transform.gameobject.GetComponent in line 26

avatar image Tatsunomi · Jun 02, 2013 at 03:59 PM 0
Share

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.

avatar image bigbat · Jun 02, 2013 at 08:01 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

16 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges