• 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 Squalol · Aug 04, 2015 at 08:28 PM · getcomponent

GetComponent returning null. Works in one object but not in another.

Hello, i know this error gets asked a lot, but im posting this because the other posts havent helped me. I got the following code on the player gun:

 if(Physics.Raycast (shootRay, out shootHit, range, shootableMask))
         {
             EnemyHealth enemyHealth = shootHit.collider.GetComponent <EnemyHealth> ();
 
             if(enemyHealth != null)
             {
 
                 enemyHealth.TakeDamage (damagePerShot, shootHit.point);
                         }
 
             
         }
 

And it WORKS. If the raycast hits an enemy, it accesses its EnemyHealth script and it takes damage. My problem comes when i try to use the SAME CODE in an automated turret. The turret does shoot at the enemy, and the raycast correctly hits it (ive used debug.log to make sure its hitting the enemy) but the getcomponent always returns null on the turret. I have even tried to make the enemyHealth in "TurretShooting" public and dragging and dropping the enemy object on it in the inspector, but even tho it lets me drop it (the forbidden sign doesnt appear), it instantly disappears when i let go of the mouse click. (on the player i can drop it and works flawlessly)

Here are some screenshots of how i have it set up. Its based on the Unity Survival Shooter Tutorial:

ENEMY with the EnemyHealth script attached.

alt text

TURRET

alt text

PARTS OF THE TURRET THAT ROTATE AND SHOOT (The code pasted above is in TurretShooting script.

alt text

Object that the player uses to shoot

alt text

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
1

Answer by getyour411 · Aug 04, 2015 at 09:41 PM

Your code does not check WHAT the raycast hit. The raycast from the turret is hitting something else (add debug.log immediately after to see what) and thus the NullRef

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 Squalol · Aug 04, 2015 at 10:10 PM 0
Share

I didnt upload the screenshot with the Debug but here it is, you can see it hits Enemigo alt text

avatar image getyour411 · Aug 05, 2015 at 02:09 AM 0
Share

Alright, but I don't see there error you said you always get here in the console output.

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

2 People are following this question.

avatar image avatar image

Related Questions

Having trouble understanding parenting with componants 0 Answers

GetComponent is not member of Object? 2 Answers

Is GetComponent<>() slow? 1 Answer

How to reduce usage of GetComponent()? (instantiate, prefabs) 0 Answers

Get inspector element using c# 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