Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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
0
Question by MorphingDragon · Apr 02, 2013 at 04:46 PM · scene-loadingnullinitialize

Application LoadLevel() not working as expected

I have a quick prototype that I've whipped up in Unity but I'm having issues with scene transitions. I have a Game Scene and a Start Scene, akin to those flash game loading screens that have instructions or whatever. The issue I'm having is when I use Application.LoadLevel to load the game scene from the starting scene, game objects don't seem to be initialized and my game crashes with null exceptions. When I run the game scene from the editor everything works fine. What am I doing wrong?

My code pretty much is this:

   if (GUI.Button(new Rect(100.0f, 50.0f, 100.0f, 50.0f), "Play Defending"))
         {
             GameController.clientColour_ = PlayerEnum.Blue;
             Application.LoadLevel("gameScene");
         }
 
         if (GUI.Button(new Rect(210.0f, 50.0f, 100.0f, 50.0f), "Play Attacking"))
         {
             GameController.clientColour_ = PlayerEnum.Red;
             Application.LoadLevel("gameScene");
         }

 
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 perchik · Jul 26, 2013 at 08:56 PM 0
Share

So you're trying to set some parameters of GameController that are used in the next scene? How is GameController defined?

I think you'll have to create some kind of persistence to keep the game objects around through multiple scenes [if I'm understanding your question correctly]

3 Replies

· Add your reply
  • Sort: 
avatar image
-1

Answer by logang · Apr 02, 2013 at 05:40 PM

Here try this:

if (GUI.Button(new Rect(100.0f, 50.0f, 100.0f, 50.0f), "Play Defending"))

{

GameController.clientColour_ = PlayerEnum.Blue;

Application.LoadLevel(1);

}

if (GUI.Button(new Rect(210.0f, 50.0f, 100.0f, 50.0f), "Play Attacking"))

{

GameController.clientColour_ = PlayerEnum.Red;

Application.LoadLevel(1); //in build settings look at the scene you want to load to and set the number of the scene to that loadlevel

}

that should work

Comment
Add comment · Show 4 · 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 EliteMossy · Apr 02, 2013 at 09:07 PM 0
Share

please use code tags.

avatar image MorphingDragon · Apr 02, 2013 at 09:44 PM 0
Share

All that does is change the level to be indexed by its build number rather than its name. That wont solve my issue. It loads the right level, but nothing seems to be initialized.

avatar image rupeshpamaihgari · Jul 26, 2013 at 08:53 PM 0
Share

same problem for me with android device..!

avatar image Murkas · Aug 08, 2013 at 11:25 PM 0
Share

I've got the same / simliar problem. The first loading of my main menu is working, but if I am (after playing) want to go pack to the main menu, I only get a blue screen. Scripts are still working, so the main camera is initialized (I've attached a script to it, which checks Input), but I can't find it through GameObject.Find($$anonymous$$ain Camera). [...] Camera.main.gameobject. [...] . In both cases I only get a NullRefereneException. That is only happening as Standalone, in the editor everything is working perfectly. Also some days ago nearly the same scene was working without any problems. I only added one gameobject.

EDIT: Some builds later I just tried it again and now it is working again.. curious bug..

avatar image
0

Answer by cmpgk1024 · Aug 08, 2013 at 11:32 PM

To have objects persist between scenes you need to use `Application.LoadLevelAdditive`. Your null reference exceptions are likely coming from GameController not persisting.

Comment
Add comment · Show 1 · 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 Murkas · Aug 08, 2013 at 11:37 PM 0
Share

I think you don't understand the question. That is not the problem.

avatar image
0

Answer by GroutchoGG · Apr 02, 2015 at 05:43 AM

You have to wait till the next frame before you can access the loaded level objects and properties.

You can do that in a coroutine :

 Enumerator LoadLevelRoutine(string levelName)
 {
     Application.LoadLevel(levelName);
     yield return null; // let a frame pass

     // now you can access the scene objects and properties
 }

Then, in your code, just do :

 StartCoroutine(LoadLevelRoutine("my level"));

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.

Follow this Question

Answers Answers and Comments

17 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

Related Questions

Application LoadLevel() not working as expected 0 Answers

Default string value... empty? (C#) 2 Answers

Problems with Playerprefs script 1 Answer

Switching between scenes, and saving their states 0 Answers

Go through door, by loading next scene, with key-press 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