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 LakeTinn · Oct 21, 2013 at 01:28 AM · raycastreferencenullexception

NullRefernceExcepception with Raycasting

Run-Time Error:

 NullReferenceException: Object reference not set to an instance of an object
 Shooting Script.ForceFire () (at Assets/Scripts/Shooting Script.js:170)
 Shooting Script.Update () (at Assets/Scripts/Shooting Script.js:108)

Function Causing Error:

 function ForceFire () {
         audio.PlayOneShot(shotSound); // play the shot sound
         var hit : RaycastHit;
         var ray : Ray = Camera.main.ScreenPointToRay(Vector3(Screen.width * 0.5, Screen.height * 0.5, 0));
         if (Physics.Raycast(ray, hit, 100)){
             var particleClone = Instantiate(bloodPrefab, hit.point, Quaternion.LookRotation(hit.normal));
             Destroy(particleClone.gameObject, 2);
             hit.transform.SendMessage("ApplyDamage", damage, SendMessageOptions.DontRequireReciever);
         }
                         }

Complete Code: http://pastebin.com/BjicFJji

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Polantaris · Oct 21, 2013 at 01:41 AM

You have to ensure that the raycast has actually hit something before you use it.

RaycastHit objects will still be created, regardless of if it actually resulted in a collision. As a result, you must check to see if it's actually referencing anything before you begin to do anything with it.

 function ForceFire () {
        audio.PlayOneShot(shotSound); // play the shot sound
         var hit : RaycastHit;
         var ray : Ray = Camera.main.ScreenPointToRay(Vector3(Screen.width * 0.5, Screen.height * 0.5, 0));
         if (Physics.Raycast(ray, hit, 100)){
             var particleClone = Instantiate(bloodPrefab, hit.point, Quaternion.LookRotation(hit.normal));
             Destroy(particleClone.gameObject, 2);
             if(hit.transform) {
                 hit.transform.SendMessage("ApplyDamage", damage, SendMessageOptions.DontRequireReciever);
             }
         }
   }

If the Raycast failed (IE it returned false), then hit.transform would be null. That's why you're getting a NullReferenceException. hit exists, because you set it to the output of the Raycast, however hit.transform does not exist, because the Raycast failed and hit nothing.

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 LakeTinn · Oct 21, 2013 at 02:06 AM 0
Share

I've applied your code but it still has the same error as before. Code for what is receiving Raycast if it helps: http://pastebin.com/FCJd1Zfh

avatar image Polantaris · Oct 21, 2013 at 02:23 AM 0
Share

Which line is Line 170 on your original NullPointerException?

NullReferenceException: Object reference not set to an instance of an object

Shooting Script.ForceFire () (at Assets/Scripts/Shooting Script.js:170) Shooting Script.Update () (at Assets/Scripts/Shooting Script.js:108) This basically says that it encountered an error on Line 170, while executing the method "ForceFire" which was executed by "Update" on line 108. This means that Line 170 is the place where the exception occurred. If 170 is the line before the if(hit.transform), then it's because of another reference to the RaycastHit (hit.point or hit.normal). I've never used RaycastHit.point nor RaycastHit.normal before, they may be null for some reason as well. I would add another check on hit.point, as well as hit.normal, to make sure that they are not null before you attempt to use them. Also, are you sure that the other things you are referencing are not null? For example, has bloodPrefab been properly defined as well? Any of those references could result in a NullReferenceException if not properly defined. If you tell us which line is 170, it will be easier to deter$$anonymous$$e which possible references are at fault here.

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

15 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

Related Questions

I am getting the error of NullReferenceException? 1 Answer

Despawner script returns null reference exception. 0 Answers

Why is the Object reference is empty ? 1 Answer

NullReferenceException Error 6 Answers

Instantiate() as GameObject = null reference 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