• 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 Klies · Feb 21, 2010 at 06:31 AM · errornullreferenceexception

Odd NullReferenceException error. Help appreciated

I'm having problems with a null reference error that seems to be stemming from this condition: if(closestInteractable !== null)

The exact error is NullReferenceException: Object reference not set to an instance of an object.

I'm not sure why it thinks it doesn't have an object tied to it, as after coming back from the FindClosestInteractable call the print statement says that it is an Interactable.

Any help would be appreciated.

private var sceneInteractables : Interactable[]; private var closestInteractable : Interactable; private var guiEnabled = false; private var interactKey = "k"; var interactDistance = 10;

var textOffset : Vector2;

function Awake() { sceneInteractables = GameObject.FindObjectsOfType(Interactable); closestInteractable = null; //guiEnabled = false; print("Size of interactable array is " + sceneInteractables.length); }

function Update () { closestInteractable = FindClosestInteractable();

 //print(closestInteractable);

 if(closestInteractable !== null) {
     //print("if statement is true");
     guiEnabled = true;
 }
 else {
     guiEnabled = false;
 }

 if(Event.current.type == EventType.keyDown && Event.current.character == interactKey && closestInteractable != null)
 {
     closestInteractable.Interact();
 }

}

function FindClosestInteractable () : Interactable { var closest : Interactable; closest = null; var distance = Mathf.Infinity; var position = transform.position; // Iterate through them and find the closest one for (var interactable : Interactable in sceneInteractables) { var diff = (interactable.transform.position - position); var curDistance = diff.sqrMagnitude; if (curDistance < distance && curDistance < interactDistance) { closest = interactable; distance = curDistance; } } print(closest); return closest; }

function OnGui() { if(! guiEnabled) return;

 GUI.Label(Rect(textOffset.x, textOffset.y, closestInteractable.InteractString.length * 10, 10), closestInteractable.InteractString);

}

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 Klies · Feb 21, 2010 at 07:08 AM 2
Share

I've fixed the problem. Using Event ins$$anonymous$$d of Input is a bad idea inside of the Update function. Also there were several other fun errors scattered about.

avatar image Nicolaj Schweitz · Feb 23, 2010 at 11:41 AM 0
Share
  • Then post it as an answer, please. ;-)

1 Reply

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

Answer by duck · Feb 21, 2010 at 07:08 AM

The only thing I can see wrong with your code is that you have !== when you should have !=.

Perhaps this causing the problem (although I'm not sure what !== does, and why it's not throwing a parsing error itself).

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

No one has followed this question yet.

Related Questions

Null Reference Exception Error. Modified Stats. Need Help. 1 Answer

NullReferenceException problem 2 Answers

NullReferenceException 1 Answer

[answered] How to fix an error on my Item Collection Script? 1 Answer

3d Platform Tutorial Help 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