Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Additive Scene Won't Unload

Discussion in '2D' started by Cobble, Nov 5, 2016.

  1. Cobble

    Cobble

    Joined:
    May 9, 2015
    Posts:
    33
    Hi! I used a script to load an Additive scene, and the script worked. But now I'm trying to use a script to Unload said scene, but it's not working.

    Here's the script I used to load the Additive scene

    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.SceneManagement;
    4.  
    5. public class LoadMiniGame : MonoBehaviour {
    6.  
    7. public string loadLevel;
    8.  
    9. void OnTriggerEnter2D(Collider2D other) {
    10.  
    11. if (other.gameObject.name == "Scarlet") //NotethatyoucanjustuseCollider2D.namehereifyou want
    12. {
    13. Debug.LogError("Collisiondetected,TryingtoLoad");
    14. SceneManager.LoadScene(loadLevel, LoadSceneMode.Additive);
    15. }
    16. }
    17. }
    And here's the script I used to Unload that scene.

    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.SceneManagement;
    4.  
    5. public class Unload: MonoBehaviour {
    6.  
    7. public static bool UnloadScene;
    8. string sceneName;
    9.  
    10. void OnTriggerEnter2D(Collider2D other) {
    11.  
    12. if (other.gameObject.name == "Scarlet")
    13. {
    14. Debug.LogError("Collisiondetected,TryingtoLoad");
    15. SceneManager.UnloadScene (sceneName);
    16. }
    17. }
    18. }
    I spent a while trying to figure out what's wrong with the second script, but I can't figure it out. Can anyone help?

    When the player collides with the object that's supposed to upload the scene, I get this error.

    Collision detected, Trying to Load
    UnityEngine.Debug:LogError(Object)
    Unload:OnTriggerEnter2D(Collider2D) (at Assets/Scripts/Unload.cs:14)
     
  2. Hyblademin

    Hyblademin

    Joined:
    Oct 14, 2013
    Posts:
    725
    From the documentation:

    ...but I don't see any asynchronous unload anywhere in the API. I'm not really sure what's up with that. Maybe you could use OnTriggerEnter2D to set a flag which causes UnloadScene to run in Update()?

    Edit: Here it is, it's in the 5.5b API. Are you using the 5.5 beta by any chance?

    (https://forum.unity3d.com/threads/scenemanager-unloadsceneasync.436631/)
     
    Last edited: Nov 7, 2016
  3. Cobble

    Cobble

    Joined:
    May 9, 2015
    Posts:
    33
    Oops, forgot about this thread. I found out how to make it work on my own. But thanks anyway!
     
  4. Hyblademin

    Hyblademin

    Joined:
    Oct 14, 2013
    Posts:
    725
    Post your solution here! Spread the word for those who are having the same problem as you were.
     
  5. UnanchoredStudios

    UnanchoredStudios

    Joined:
    Jan 26, 2020
    Posts:
    6
    exactly