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 Sooper1337 · Oct 02, 2012 at 01:59 PM · c#raycastnullreferenceexception

Object reference not set to an instance of an object

I am trying to set up a script for a rail shooter that uses raycasting for shooting instead of just cloning objects . everything worked except for when the raycast collides with the enemy it throws this error

NullReferenceException: Object reference not set to an instance of an object Player.RaycastShoot () (at Assets/Player.cs:50) Player.Update () (at Assets/Player.cs:65)

im trying to get it to run the function in a script on an enemy. or if there is an easier way to set it up to deduct health from the enemy on each hit i would greatly appreciate knowing.

 public void RaycastShoot ()
     {
         Enemy enemyObj;
         enemyObj = gameObject.GetComponent("Enemy") as Enemy;
         
         Vector3 _ray = new Vector3(Input.mousePosition.x,Screen.height - Input.mousePosition.y - 100,0);
         
         Ray ray = Camera.main.ScreenPointToRay(_ray);
         
         RaycastHit hit;
         
         if(shootTimer > 0)
             shootTimer -= Time.deltaTime;
         
         if(shootTimer <= 0)
             shootTimer = 0;
         
         if(shootTimer == 0)
         {
             if(Input.GetKey(KeyCode.Mouse0))
             {
                 shootTimer = shootDelay;
                 
                 if(Physics.Raycast(ray, out hit, 100f))
                 {
                     
                     Debug.DrawRay(_ray, transform.TransformDirection(Vector3.forward), Color.green);
                 
                     GameObject otherObj = hit.collider.gameObject;
             
                     // Part of the code that is throwing an error
                     if(otherObj.tag == "Enemy")
                     {
                         enemyObj.EnemyDmg(damage);
                         
                         print ("Hit an Emeny");
                     }
                 }
             }
         }

this is the line that is causing the error

 if(otherObj.tag == "Enemy")
               {
                  enemyObj.EnemyDmg(damage);
 
                  print ("Hit an Emeny");
               }
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 Dave-Carlile · Oct 02, 2012 at 03:18 PM 0
Share

Which specific line of that code is throwing the exception?

avatar image Dave-Carlile · Oct 02, 2012 at 03:39 PM 0
Share

Seems like either otherObj or enemyObj is null. $$anonymous$$y guess would be enemyObj. The line towards the top where you set enemyObj: enemyObj = gameObject.GetComponent("Enemy") as Enemy;

Are you sure it's finding what you expect? Before the code that's causing the error, do something like: if (otherObj == null) print "otherObj is null"

if (enemyObj is null) print "enemyObj is null".

Once you find out which one is null, it's on to finding out why.

1 Reply

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

Answer by kmeboe · Oct 02, 2012 at 05:25 PM

Try changing your code to this:

   // Part of the code that is throwing an error
   if(otherObj.tag == "Enemy")
   {
          Enemy hitEnemyObj = otherObj as Enemy;
          if (hitEnemyObj)
          {
              hitEnemyObj.EnemyDmg(damage);
             print ("Hit anemony");
          }
          else Debug.LogError("Error: hit an object with tag \"Enemy\" that is not an Enemy!");
   }
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.

Follow this Question

Answers Answers and Comments

13 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

Related Questions

AI raycasting problem 0 Answers

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

NullReferenceException when an object is in the way that is not the player 1 Answer

Picking Weapons Up with Raycast 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