Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Gaming_Project · Aug 31, 2016 at 05:39 AM · scripting problemloadlevelscene loadloadlevelasyncscene change

Execute script when scene is visible to player

Hi all

When using scenemanager I am having differences between editor and device. I have a singleton game manager which sets up some logic and then loads next scene which is the menu scene.

On that menu scene I have a countdown script which starts at 10 down to 1. This is hooked up to a Unity UI text control and updates the correct count in the editor as expected.

However, when I check on a device the menu scene loads but there is a bigger delay and by the time I can see the new scene the countdown is already down to 4.

How can I consistently manage the transition so that I only start scripts / animations when visible to cater for the different loading speeds and behaviours between hardware?

I have tried scenemanager.loadsceneasync as well as AsyncOperation such as allowSceneActivation to false as well as checking isDone and none have the desired effect. Also OnLevelWasLoaded has an unpredictable execution order.

What would you suggest for a true "is visible to user" and "is fully loaded" check to kick off these scripts?

Comment
Add comment · Show 3
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 Eragoth · Aug 31, 2016 at 04:18 PM 0
Share

Have you tried to put GameObject with a script attached on it? So it's runs whenever the scene is loaded. Just my guess.

avatar image Gaming_Project · Aug 31, 2016 at 07:49 PM 0
Share

I have indeed and that is what is executing the countdown but start, update or any other such method doesn't mean (or guarantee) its available to view to the player.

avatar image Gaming_Project · Sep 02, 2016 at 08:08 AM 0
Share

I have also tried renderer.isVisible and also OnBecameVisible member on the object and still get the inconsistencies across devices.

Any suggestions?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by RicoLoco · Sep 02, 2016 at 01:13 PM

I believe the counter shouldn't start until allowSceneActivation is set to true unless the counter logic is on the Singleton rather than an object of the newly loaded scene...

Do you do any sort of visual transition? At the end of a fade or something could be a good time to activate the counter.

Comment
Add comment · Show 3 · 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 Gaming_Project · Sep 02, 2016 at 01:46 PM 0
Share

Thanks for the comment @RicoLoco

No visual transition between scene at this stage. I tried the allowSceneActivation with code similar to this:

     private void StartNow () {
         AsyncOperation async = Application.LoadLevelAsync (1);
        
         // Set this false to wait changing the scene
         async.allowSceneActivation = false;
  
         StartCoroutine (LoadLevelProgress (async));
     }
    
     IEnumerator LoadLevelProgress (AsyncOperation async) {
         while (!async.isDone) {
             Debug.Log ("Loading " + async.progress);
             yield return null;
         }
        
         Debug.Log ("Loading complete");
        
         // This is where I'm actually changing the scene
         async.allowSceneActivation = true;
     }

but put the value of async.allowSceneActivation to a variable on the singleton which I could access from my countdown object.

In that object within the update method I am checking that allowSceneActivation is true to instigate the countdown. All this works fine within the editor but skip to the device and the countdown which starts at 10 is only visible from 4 or 5.

Is this approach correct?

avatar image RicoLoco · Sep 02, 2016 at 07:45 PM 0
Share

Application.LoadLevel has been upgraded. Here's a good explanation of the new way where you can take advantage of additive scene loading and I'm no expert but I would bet that that would solve your problem. Best advice I can offer. Good Luck.

http://www.alanzucconi.com/2016/03/23/scene-management-unity-5/

avatar image RicoLoco · Sep 03, 2016 at 04:48 AM 0
Share

Here's something I just stumbled on that sounds like it may help, the RuntimeInitializeOnLoad$$anonymous$$ethodAttribute. It's brand new to me but I thought of your problem when I found it so thought I'd share... http://docs.unity3d.com/ScriptReference/RuntimeInitializeOnLoad$$anonymous$$ethodAttribute.html

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Scene changing with virtual button 1 Answer

How to save changes to a scene when moving to another scene 1 Answer

Loading Scene in between two scene for web player. 0 Answers

LoadSceneOnClicks.LoadAsynchronously(int)': not all code paths return a value 1 Answer

Room change error in multiplayer 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