• 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
Question by rkGio · May 30, 2014 at 10:52 PM · nullreferenceexceptionnull

How can this possibly be null?

     mySquare = startSquare;
     if(mySquare == null) print("WHAT?");
     
     while(mySquare != myGoal)
     {
         if(mySquare == null) print("Square Null");
     }

mySquare is not null before the While ("WHAT?" never gets printed), however, somehow, mySquare becomes null right after the while ("Square Null" always gets printed).

Can anyone explain to me how is that even possible?

Comment

People who like this

0 Show 7
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 tw1st3d · May 30, 2014 at 10:56 PM 0
Share

I'm going to go on a hunch and say you're changing the value of mySquare in your Update() method or something of the like.

avatar image rkGio · May 30, 2014 at 10:59 PM 0
Share

I am not, this script doesn't even have an Update()

avatar image Lovrenc · May 30, 2014 at 10:59 PM 0
Share

@tw1st3d: I thought actions in unity run sequentialy

avatar image tw1st3d · May 30, 2014 at 11:03 PM 0
Share

If it's a Coroutine that has been yielded, the Update() method could have very well changed the variable value by the time the yield ends (by all means of logic, I have no research to prove it).

avatar image rkGio · May 30, 2014 at 11:05 PM 0
Share

But, there is no Update() in the script, furthermore, mySquare only exists within this function as a local variable, and is only changed within the While loop.

Show more comments

2 Replies

· Add your reply
  • Sort: 
avatar image

Answer by Jeff-Kesselman · May 30, 2014 at 11:07 PM

Is this really ALL the code? Because it looks like an idiot loop as presented. If not your problem is probably in the code you didn't bother to show us.

Assuming that this code is really the code you are running, then the answer has got to be that the variable is being changed by another thread. Make sure the variable is a local, not a global, to make sure something else isn't setting it.

Otherwise its probably a lot more likely that either the code you are showing us is not the same code, or that its not really the code that is generating your output -- there is either another copy of the code out there in another script OR you have attached this script to more then one object OR you have attached on an object more the once.

When you see something "impossible" in programming it ALWAYS means one of your assumptions is wrong. So go through all your assumptions one by one and start proving them right. You can start by showing us what you assumed was unimportant in the above code.

Comment

People who like this

0 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 rkGio · May 30, 2014 at 11:14 PM 0
Share

The script is only attached to the player, I just checked and I only attached it once to the player, and the whole script is as follows:

  function Awake(){
     
     squareArray = GetComponent(Character).squareArray;
     }

 function MakePath(startSquare : Square, endSquare : Square)
 {
     var mySquare : Square;
     var myGoal : Square = endSquare;

     mySquare = startSquare;
     if(mySquare == null) print("WHAT?");
     
     while(mySquare != myGoal)
     {
         if(mySquare == null) print("Square Null");
     }
 }
avatar image rkGio · May 30, 2014 at 11:30 PM 0
Share

It is indeed the whole code (except a "var squareArray : List." before the Awake function); I know it is just an idiot loop doing nothing but I was just "testing the water" before adding any real code in.

avatar image

Answer by Lovrenc · May 30, 2014 at 11:30 PM

There are couple of possibilities.

1) Your value is changed by a different thread. From the snippet I suspect you are fairly new coder so it might be the code you copied/included from third party.

2) Square Null prints so fast, "What?" string goes out of bounds (if I remember correctly console holds 500 outputs) and your conclusions are wrong.

I can't think of anything else from what little you showed here.

Comment

People who like this

0 Show 5 · 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 tw1st3d · May 30, 2014 at 11:32 PM 0
Share

I agree with 2. @rkGio, try changing your while statement to

 if(mySquare != myGoal){ 
  if(mySquare == null) {
   print("Square Null");
  }
 }
avatar image Jeff-Kesselman · May 30, 2014 at 11:38 PM 0
Share

Thats a good idea. Also turn on collapse on your output. That will prevent repeated identical error messages from filling up your console.

alt text

avatar image rkGio · May 30, 2014 at 11:45 PM 0
Share

@tw1st3d That did work, mySquare is no longer null. But, I'll need to use While when I implement the real code.

@Lovrenc I'm not using any third party code

avatar image tw1st3d · May 30, 2014 at 11:46 PM 0
Share

If it worked my way, then it's working your way, but it's logging too fast for you to see it. There's no technical problem with the code, just the delivery of logging.

avatar image Lovrenc · May 31, 2014 at 12:05 AM 0
Share

So it is point 2. Value of the mySquare variable never changes. It was null from the beginning. Everything works as intended.

Also, what @tw1st3d said in comment above is true and since you failed to realize this on your own I suggest you read about basic looping before you go on. (I am not saying that with bad intentions).

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

24 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 avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

NullReferenceException question. 2 Answers

3rd person controller NullReferenceException 2 Answers

Checking if an object is null, results in NRE 1 Answer

Help with raycast == null 0 Answers

Raycast causes game to crash when shooting up 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