• 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
11
Question by Ray-Pendergraph · Aug 05, 2010 at 07:14 PM · nullreferenceexceptionmono

LoadLevelAdditive requires one frame to initialize scene objects.

Renamed from: Null but not null (equality override problem)?

I am dealing with a problem I just can't get to the bottom of. Actually a co-worker of mine (oddly enough) is dealing with the exact same issue on a totally unrelated area.

        GameObject sceneRoot = GameObject.Find(levelName);
        Debug.Log(sceneRoot + " "+(sceneRoot == null));

The output that this produces is

UnityEngine.GameObject True

How can this be? We have been researching this problem all day. In my case the object named "levelName" has been added to the scene graph via additive scene loading. I can see it on the scene graph as it gets added. The stack trace suggests the object is there and the null is coming from the inside of the game object. What would cause this odd behavior? Do these objects not get really initialized until the end of the frame or something?

Additional note: Trying to call GetComponent on this yields:

NullReferenceException
UnityEngine.GameObject.GetComponent[Transform] () [0x00000]

I get the NRE, but it's like its coming from withn GetComponent... my code should be at the top of the stack, not GameObject's.

Edit(8/5): This looks like the default behavior of Find upon more research. This is very strange to me:

GameObject go = null;
Debug.Log("Object "+go + " " + (go == null));
go = GameObject.Find("TestScene2");
Debug.Log("Object " + go + " " + (go == null));

Yields this:

Object  True
Object UnityEngine.GameObject True

So I can print it but it's null?

Edit(8/5 ... later in the day):

int frame = 1;
void Update()
{
    if (frame == 1)
    {
        Application.LoadLevelAdditive("TestScene2");
    }
    GameObject go = GameObject.Find("AnObjectInTestScene2");
    UnityEngine.Debug.Log(frame++ +" "+go + " " + (go == null));
}

Yields this:

1 UnityEngine.GameObject True
2 UnityEngine.GameObject False
...

So this tells me I cannot call Application.LoadLevelAdditive and use something in the scene graph immediately. Find only works on the frame after the load?

Workaround: After some more research and the comment from SpikeX I decided to use the Async version of LoadLevelAdditive, this way I am not relying on wall time to accomplish this. I just had to add some more complexity and gears to deal with the async. This would be a nice thing to get documented in the docs. The explicit presence of LoadLevelAdditiveAsync makes one believe that LoadLevelAdditive is truly sync, but it's truly not.

Comment
Add comment · Show 1
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 Vicas · May 12, 2014 at 06:32 AM 0
Share

Thank you for this, I had this exact problem, and did the exact reasoning of thinking LoadLevelAdditive was truly sync.

2 Replies

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

Answer by qJake · Aug 05, 2010 at 09:07 PM

Your findings are correct, but as far as I know it's not a bug, it's just how Unity was designed to work. Take this test case I just tried:

using UnityEngine; using System.Collections;

public class Test : MonoBehaviour {

 void Start()
 {
     Invoke("DoTest", 3); // just to add some delay
 }

 void DoTest()
 {
     // METHOD 1 - Doesn't work!

     //Application.LoadLevelAdditive("Scene 2");
     //GameObject go = GameObject.Find("Object B");
     //Debug.Log("GO is " + ((go == null) ? "null" : "not null"));

     // METHOD 2 - Does work :)

     StartCoroutine(TestTwo());
 }

 IEnumerator TestTwo()
 {
     Application.LoadLevelAdditive("Scene 2");
     yield return 0; // or yield return new WaitForEndOfFrame();
     GameObject go = GameObject.Find("Object B");
     Debug.Log("GO is " + ((go == null) ? "null" : "not null"));
 }

}

If you use Method 1 (which doesn't work, and is commented out), it prints GO is null. But if you use the current, uncommented method 2, it prints GO is not null. You may think this is strange behavior, but it's not.

So my understanding of how Unity works is, it takes everything you do in a script, in one frame (one Update()), and it batches it all together, and then executes that at the end of the frame. So when you tell it to load the level, and then immediately find the object, it can't, because it's added the "LoadLevelAdditive" call to its queue, but it executed the "FindObject" immediately (because it needs the reference for the script). The solution is to use a coroutine, and let one frame pass before trying to read any objects you loaded with LoadLevelAdditive.

Oh, and by the way, when you concatenate a null object with a string it just converts it to "", which is why you can still print it.

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 Ray-Pendergraph · Aug 06, 2010 at 12:37 AM 2
Share

So it's official then. Thanks for the answer. I put my workaround comments in the post. I want to actually open a new topic on the Null vs not null thing though, "== null" is true on the GameObject but it still ToString()s. I have a sneaking suspicion that == has been changed there.

avatar image Mike 3 · Aug 06, 2010 at 08:31 AM 0
Share

They overload == to make some internal stuff work, but you should trust it when it reports that it's equal to null, it means that it either is null, or the internals are currently null which will lead to a null reference exception anyway

avatar image
2

Answer by jashan · Aug 06, 2010 at 05:58 AM

Regarding your null is not null issue, you might want to have a look at this (pretty old) forum posting of mine: Huh? varName != null is false, when varName is not null?

I ran into that issue when instantiating game objects / scripts via constructors (which is something you shouldn't do). And yeah, I think you're right: It has to do with equals ("==") being overridden for some "internal magic" ;-)

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 Ray-Pendergraph · Aug 06, 2010 at 11:47 AM 1
Share

Thanks for the link Jashan. This post accidentally turned out to be two questions (I hate it when people do that :-) ). I was going to open another thread for this but seems I don't need to.

avatar image jashan · Aug 06, 2010 at 01:53 PM 1
Share

Well, that can always happen ... but I know what you mean ... I considered waiting for your new question but then had the link ready already so I decided to post it right here ;-)

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

1 Person is following this question.

avatar image

Related Questions

Can Unity Iphone game be written in C#? 4 Answers

Is it possible to upgrade the included version of Mono manually? 1 Answer

Integration with DarkStar/RedDwarf? 1 Answer

Mono Runtimes Not Detected After Install 2 Answers

sqlite plugin anomalies on ios 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