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
1
Question by Slawo · Jan 29, 2014 at 12:22 PM · sceneloadingstartup

Appropriate way to force the loading of a particular scene at startup

I'm trying to force the application to load a particular Scene "loading" whenever the player is started.

For this I use a static class like so:

 [InitializeOnLoad]
 public class Autorun
 {
     static Autorun()
     {
         String levelName = "loading";
         Debug.Log("Autorun will load " + levelName);
         Application.LoadLevel(levelName);
     }
 }

Unfortunately the Application.LoadLevel(levelName); seems to be completely ignored.

If this is to be expected I would like to know how I can achieve the Load at startup.

edit: The static constructor is executed at startup. I suspect Application is not ready when I call the LoadLevel which would explain this behaviour. How should I proceed to run my code (save and preferences loading, network queries ad initial scene display) as soon as Application has finished initialising its dependencies?

Thanks.

Comment
Add comment · 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 CHPedersen · Jan 29, 2014 at 01:19 PM 0
Share

I added an answer that totally misread the static constructor and took it as a static method call without a return type. :) This was plainly wrong - I've deleted the answer again. Sorry about the inconvenience. :P

As a guess: Perhaps the call to Application.LoadLevel fails because the Unity runtime isn't in a state where it is ready to receive and handle Application.LoadLevel at the time the .Net runtime executes static constructors?

avatar image Slawo · Jan 29, 2014 at 01:24 PM 0
Share

@CHPedersen Indeed. I too suspect the Application is still initialising or maybe has not even started yet. Do you know if there is some sort of callback system to get notified when the Application is ready?

avatar image CHPedersen · Jan 29, 2014 at 01:29 PM 0
Share

I think I'd try to stick to executing code in the $$anonymous$$onoBehavior callbacks. It seems Unity's design expects us to use these as entry-points for user-code rather than static constructors.

There is a callback, OnLevelWasLoaded which is called whenever a level is loaded, and the application thus presumably ready. But again, this requires you to construct some scene with a gameobject to host the script that handles this callback, in which case you might as well stick your code in that script's Awake-method. This, I believe, is the very first thing that gets called when an object is first initialized.

avatar image njpatter · Jan 29, 2014 at 01:46 PM 1
Share

You could create a scene specifically for loading and then use PlayerPrefs to store which scene to load on startup. It could just be a quad with 'Loading' on it along with the loader script and a camera pointed at it.

avatar image gfoot · Jan 29, 2014 at 02:28 PM 0
Share

In case it's not obvious or already known to you, Unity already has a way to define which scene gets loaded when a build of your app is executed - it's the first scene listed in the "Build Settings" dialog.

It doesn't affect what happens when you press "Play" in the editor - that always launches the currently-loaded scene. But you can fix that if you want by having an object in your current scene which checks whether the "boot" scene was already loaded, and if not, loads it straight away.

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Xtro · Jan 29, 2014 at 03:32 PM

You don't need to write code for that. Just add the Loading scene as the first scene in the build window.

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

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

Preventing first instance of object from starting on screen 1 Answer

Android game does not load fully sometimes 1 Answer

how to seperate two visually distinct game modes 1 Answer

Loading Sene problem 2 Answers

Load different Scene on first load 4 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