• 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 Harryliu · Apr 25, 2016 at 10:01 PM · javascriptraycastraycasthit

"NullReferenceException: Object reference not set to an instance of an object" When Raycast hits and tries select a designated object

This script is acting strange... I have tried solutions from other answers but they don't seem to work at all. I have a ray that constantly checks for GameObjects with the layer "Interactable". The ray has no difficulty identifying the objects, but when it tries to selected it and store it in a variable for further purposes, it returns this error: "NullReferenceException: Object reference not set to an instance of an object"

Here's the script:

 #pragma strict
 
 var cam : Transform;
 var layerMask : LayerMask;

 function Update () {
 
     var ray = new Ray(cam.position, cam.forward);
     var hit : RaycastHit;
 
     if(Physics.Raycast(cam.transform.position, cam.forward, 100, layerMask))
     {
         var selectedObject : GameObject = hit.transform.gameObject;
         Debug.Log("You hit something");
         
     }
 }

I've also tried this but it didn't work either:

var selectedObject : GameObject = hit.collider.gameObject;

What is wrong? Thanks!

Comment
Add comment
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

2 Replies

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

Answer by Eno-Khaon · Apr 25, 2016 at 11:34 PM

You're not actually assigning anything to "hit".

When you fire a Raycast, you need to ensure that you use the variant of the function in which the outgoing variable is read and modified.

 if(Physics.Raycast(cam.transform.position, cam.forward, 100, layerMask))
 // Change to...
 if(Physics.Raycast(cam.transform.position, cam.forward, hit, 100, layerMask))
Comment
Add comment · Show 2 · 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 ntl92bk · Apr 26, 2016 at 02:15 AM 0
Share

I think u forgot "out" keyword before hit :D

avatar image Eno-Khaon ntl92bk · Apr 26, 2016 at 03:42 AM 0
Share

You definitely need it for C#, but you don't actually need if for JS.

Example:

 // C#
 if (Physics.Raycast(ray, out hit))

 // JS
 if (Physics.Raycast(ray, hit))
avatar image
1

Answer by fight_1977 · Apr 26, 2016 at 04:25 AM

u used a wrong overload of Physics.Raycast, no out parameter named hit in the function parameter lists. so hit is a undefined variable, then the NullReferenceException error.

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

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

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

Related Questions

Raycast script help? 1 Answer

Switch GameObjects Tags with javascript 1 Answer

Make tranform follow raycast 1 Answer

detecting reycast collisions 1 Answer

Raycast Distance Help 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges