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
1
Question by basilbudgie · Apr 03, 2014 at 10:57 PM · nullreferenceexceptionraycastingnull

Raycasting null object reference error

I'm fairly new to scripting in Unity, and I'm creating a game where I want to change cameras when an object is clicked on. I'm not focusing on the code for changing cameras at the moment, but instead detecting if the object has been clicked on. I've looked around and modified some code from other places to use Raycasting. Here is my code:

 function FixedUpdate () {
     var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
     var Hit : RaycastHit;
     if(Physics.Raycast(ray, Hit, 1000)){
         if(Hit.collider == null){
             Debug.Log("miss");
         }
         else if(Hit.collider.tag == "document") {
             Debug.Log("clicked");
         }
     }
 }

I keep getting NullReferenceException: Object reference not set to an instance of an object.

I have the script attached to the first person camera in the scene. Please Help!

Comment
Add comment · Show 2
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 kilgore · Apr 03, 2014 at 11:03 PM 0
Share

To say for sure I'd have to see the script in it's entirety as well as the error message Unity is throwing. You can then pinpoint what line of code Unity is complaining about.

avatar image robertbu · Apr 03, 2014 at 11:06 PM 0
Share

Pasting in the error from the console and/or indicating what line the error is occurring on would speed along a solution.

If you've changed the tag on the main camera to something other than '$$anonymous$$ainCamera', this code will fail with a null reference.

1 Reply

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

Answer by HuskyPanda213 · Apr 04, 2014 at 01:00 AM

The problem is you are checking if the hit collider is null, which is not what you want. The if(physics.Raycast) part checks if it hits something, so you are checking something that will never happen. Here is how I would do it. Two notes: First, do not call it in the FixedUpdate. Second, your naming is inconsistant, a raycasthit named Hit, with capital, but a ray without capital?

Here is how it should look:

 function Update () {
     var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
     var hit : RaycastHit;
     if(Physics.Raycast(ray, Hit, 1000)){
        if(Hit.collider.tag == "document"){
          Debug.Log("hit");
        }
        else {
 Debug.Log("You hit something else.");
 }
     }
 else{
 Debug.Log("miss");
 }
 
 }
Comment
Add comment · Show 1 · 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 basilbudgie · Apr 05, 2014 at 07:31 AM 0
Share

Thanks, that fixed it! Now I have no errors except it's not picking up the document. It says either miss or you hit something else. I think it might be a problem with my tagging. I named the object in the hierarchy view as 'document'. Is that all I had to do, or do I need to tag it, because when I look at the inspector panel it has a drop down menu that says Tag.

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

22 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 avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Best practice handling deleted object refences during SceneManager.LoadScene 0 Answers

Problem with checking if null 1 Answer

How do I nullreference this correctly? 1 Answer

NullReferenceException Error 6 Answers

How can I stop getting Null Reference Exception? 1 Answer


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