• 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 Coreyf716 · Nov 03, 2012 at 12:52 AM · javascripterrorinstance

Null Reference Exception on Raycast

 var fireSound : AudioClip;
 var reloadSound : AudioClip;
 var bullets : float;
 var reloadTimer : float;
 var coolTimer : float;
 
 function Start () {
 }
 
 function Update () {
     if (coolTimer) {
         coolTimer -= Time.deltaTime;
         if (coolTimer < 0) {
             coolTimer = 0;
         }
     }
     if (reloadTimer) {
         reloadTimer -= Time.deltaTime;
         if (reloadTimer < 0) {
             reloadTimer = 0;
         }
     }
     if (!bullets) {
         audio.PlayOneShot(reloadSound);
         reloadTimer = 3.0;
         bullets = 30;
     }
     if (bullets && !coolTimer && !reloadTimer) {
         audio.PlayOneShot(fireSound);
         coolTimer = 0.1;
         bullets -= 1;
         var hit : RaycastHit;    
         if (Physics.Raycast (transform.position, transform.TransformDirection (Vector3.forward) * 1000)) {
             if (hit.collider.name == "Character") {
                 Health.health -= 1;
             }
         }
     }
 }

This script controls the enemy weapon. Everytime I stand in front of the thing, it gives me an error message saying "Object reference not set to an instance of an object". This is not the first time I've had this show up. What is it and how do I fix the issue?

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 rutter · Nov 03, 2012 at 02:16 AM

We can be a little bit more helpful if you point out the exact line that's throwing the exception.

I'm guessing that it's this one:

 if (hit.collider.name == "Character") {

I say that because your raycast call doesn't look like it will populate the `hit` variable:

 var hit : RaycastHit; 
 if (Physics.Raycast (transform.position, transform.TransformDirection (Vector3.forward) * 1000)) {

There are many different versions of `Physics.Raycast()`, each of which has slightly different properties. You might try calling one of the versions which takes a `RaycastHit` parameter, perhaps like this:

 Physics.Raycast(transform.position, transform.TransformDirection(Vector3.forward), hit, 1000)


Comment
Add comment · Show 3 · 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 Coreyf716 · Nov 04, 2012 at 04:54 AM 0
Share

@ alucardj I'd like to give credit to you for posting the original answer. I misunderstood. Thanks for the help man

avatar image AlucardJay · Nov 04, 2012 at 05:08 AM 0
Share

no worries, am happy you found out and understood in your own way. Happy Coding =]

avatar image Coreyf716 · Nov 04, 2012 at 05:15 AM 0
Share

Haha thanks man. Same to you :)

avatar image
1

Answer by mbhaworth · Nov 03, 2012 at 08:30 AM

Look at your use of Physics.Raycast. As far as I can tell the variable hit is never set.

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

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

12 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

Related Questions

"Object reference not set to an instance of an object" Error 1 Answer

Error: you are pushing more GUIClips than you are poping. 1 Answer

Health not counting down? 1 Answer

The name 'Transform' does not denote a valid type ('not found'). Did you mean 'System.Net.Mime.TransferEncoding'? (BCE0018) 3 Answers

I need help with Javascript 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