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
2
Question by LstCenturian · Jan 10, 2018 at 12:54 AM · scripting problemscene-loadingscene-change

SceneManager.LoadSceneAsync stops coroutine

I am writing a script that controls a door trigger. When OnTriggerEnter is called, a coroutine is started to set some variables and switch the scene with SceneManager.LoadSceneAsync. When the scene is done loading, it becomes active, however the rest of the coroutine does not finish. It just immediately ends. I am not sure if this is the way the method is supposed to act, or if I am doing something wrong. If it is the way the method works, what should I do instead to get the result I want?

This is the code that is having problems:

        public IEnumerator FadeToScene(int scene, Vector2 startLoc)
        {
            PlayerController player = PlayerController.thePlayer;
            CameraFX cam = player.GetComponentInChildren
 
  ();
            player.canMove = false;
            player.rb.velocity *= 0;
            yield return StartCoroutine(cam.fadeTo(Color.black, .25F));
            Time.timeScale = 0;
  
` ` //Everything up until this point works. Below is where problems start AsyncOperation load = SceneManager.LoadSceneAsync(scene); load.allowSceneActivation = true; while (!load.isDone) { yield return null; }
//Everything below is skipped player.rb.position = startLoc; Time.timeScale = 1; yield return StartCoroutine(cam.unFade(.25F)); player.canMove = true; }

The code is in a script that is not destroyed between scenes. I have also tried moving the async load to a separate coroutine. After the scene is finished loading, the method returns to the line where it was called from, and the rest of the lines are skipped.

I am guessing that this is on purpose and I am unaware, but I am not sure.

Comment
Add comment · Show 5
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 JVLVince · Jan 10, 2018 at 01:18 AM 0
Share

Can you add some comment to your code to let us know which line of code working and which line being skipped?

avatar image LstCenturian JVLVince · Jan 10, 2018 at 01:43 AM 0
Share

Thanks for the quick reply. I've updated the code.

avatar image JVLVince LstCenturian · Jan 10, 2018 at 03:27 AM 0
Share

hmm, does any error display on your console log?I wondering that that your player gameobject has been destroyed when new scene loaded.

Show more comments

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Obsessi0n · Jan 31, 2018 at 01:16 AM

When the scene is loaded the script is destroyed you need to make it to not get destroyed during scene change.

 public class ExampleClass : MonoBehaviour {
     void Awake() {
         DontDestroyOnLoad(transform.gameObject);
     }
 }
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
avatar image
0

Answer by karmington · May 09, 2018 at 07:49 PM

Coroutines just die quiet deaths on scene changes, doesnt help if they are called out of DontDestroy objects.

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

Answer by scottpurcival · Nov 18, 2020 at 02:09 PM

Sorry for the necro. I finally figured this out, and could not find the answer anywhere online. Posting for others.

Your AsyncOperation load is being set to null. For some reason unity does not report this as a null reference error in the logger, it just quits the coroutine quietly.

Do this; while (load != null && !load.isDone) { yield return null; }

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

132 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 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 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 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 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 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

Saving and loading user made configuations 0 Answers

How to save position of stereo vrcameras 0 Answers

Multiple Scenes: Are previous scenes still active after you call a new scene? 1 Answer

SceneManager.Load is not worked in subfolders 1 Answer

Fadeout Issue 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