• 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 /
This question was closed Sep 23, 2013 at 09:33 AM by Fattie for the following reason:

Duplicate Question

avatar image
0
Question by skyworxx · Sep 23, 2013 at 08:47 AM · null referance exeption

NullReference Exception when checking for Null

I am getting an NullReference Exception when I am trying to check if a GameObject is null.

 public GameObject target; 
 [...]
 if (target != null ) doSomething();

I know that target can be null, so I am checking for null, but this check throws a NullReference Exception....

Any ideas why this is happening?

Comment
Add comment · Show 5
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 Hoeloe · Sep 23, 2013 at 08:49 AM 0
Share

Are you certain it's the check, and not the call to "doSomething()"?

avatar image skyworxx · Sep 23, 2013 at 08:54 AM 0
Share

Yes. If I write the code like this

if (target != null)

doSomething;

the error is thrown on the first line.

Also I tried an empty doSomething() method - did not help.

avatar image DeveshPandey · Sep 23, 2013 at 08:57 AM 0
Share

problem is somewhere else, so can you tell me whole error you got?

avatar image skyworxx · Sep 23, 2013 at 09:04 AM 0
Share

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

NPCBehaviour.FixedUpdate () (at Assets/AI/NPCBehaviour.cs:86) void FixedUpdate () {


if (target==null && persuer==null) searchForEnemy();
if (target!=null && //line 86 target.GetComponent().persuer!=null){ checkIfEnemyGotAway(); } if (target!=null) shoot(); calcSearchVector(); calcPersueVector(); calcEvadeVector(); calcObstacleVector(); calcTetherVector(); calcFinalVector(); Vector3 _direction = (finalVector - transform.position).normalized; Quaternion _lookRotation = Quaternion.LookRotation(_direction); transform.rotation =Quaternion.Slerp(transform.rotation, _lookRotation, Time.deltaTime * rotationSpeed); }
avatar image Fattie · Sep 23, 2013 at 09:33 AM 0
Share

It couldn't be simpler, you forgot to attach NPCBehavior.

1 Reply

  • Sort: 
avatar image
1
Best Answer

Answer by Hoeloe · Sep 23, 2013 at 09:09 AM

Even with a line break, the whole if statement is considered one line. The error is that the GetComponent call is returning null.

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

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

18 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

Related Questions

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

Detecting gameObject from 3D rayCast 1 Answer

GameObject.Find/WithTag returns null 1 Answer

i have written this code to spawn a segments of enemies i don't get errors at all but it didn't work .. what should i do ? 2 Answers

GameObject has been destroyed, but you are still trying to access it. 0 Answers


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