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. Dismiss Notice

Question Update Method isn't Called after SceneLoad

Discussion in 'VR' started by levysn, Oct 4, 2023.

  1. levysn

    levysn

    Joined:
    Aug 10, 2023
    Posts:
    5
    Hello all. I am loading an addressable scene in this script. It loads up correctly if the space-key is clicked and then should unload when the I key is clicked. However, when I attempt to unload it, it does not work and it seems as if the Update function immediately stops being called. I've even attempted to use the LateUpdate function as well as put the if-else for unLoading inside of the sceneLoader script. Does anyone have any tips?


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.AddressableAssets;
    5. using UnityEngine.InputSystem;
    6. using UnityEngine.ResourceManagement.AsyncOperations;
    7. using UnityEngine.ResourceManagement.ResourceProviders;
    8. using UnityEngine.SceneManagement;
    9.  
    10. [ExecuteInEditMode]
    11. public class TutAddressable : MonoBehaviour
    12. {
    13.     public string sceneAddress; // The address of the scene you want to load
    14.  
    15.     public AsyncOperationHandle<SceneInstance> handle;
    16.  
    17.  
    18.     private void Update()
    19.     {
    20.  
    21.         Debug.Log("update is running");
    22.        
    23.         if (Keyboard.current.spaceKey.wasPressedThisFrame)
    24.         {
    25.            
    26.             LoadSceneAddressable();
    27.         }
    28.         if (Keyboard.current.lKey.wasPressedThisFrame)
    29.         {
    30.             Debug.Log("unload called");
    31.             unLoadSceneAddressable();
    32.         }
    33.     }
    34.  
    35.     private void LoadSceneAddressable()
    36.     {
    37.  
    38.         unLoadSceneAddressable();
    39.  
    40.  
    41.         // Load the scene addressably
    42.         handle = Addressables.LoadSceneAsync(sceneAddress, UnityEngine.SceneManagement.LoadSceneMode.Single);
    43.  
    44.    
    45.         handle.Completed += operation =>
    46.         {
    47.             if (operation.Status == AsyncOperationStatus.Succeeded)
    48.             {
    49.                 // Scene loaded successfully
    50.                 Debug.Log("Scene has been loaded.");
    51.             }
    52.             else
    53.             {
    54.                
    55.                 Debug.LogError("Failed to load scene addressably: " + operation.OperationException);
    56.             }
    57.         };
    58.     }
    59.  
    60.     private void unLoadSceneAddressable()
    61.     {
    62.         if (handle.IsValid())
    63.         {
    64.             Addressables.UnloadSceneAsync(handle);
    65.             Debug.Log("Scene has been unloaded.");
    66.         }
    67.         else
    68.         {
    69.             Debug.Log("No scene to unload.");
    70.         }
    71.     }
    72.  
    73.  
    74. }
    75.  
     
  2. aquadragon3d

    aquadragon3d

    Unity Technologies

    Joined:
    Sep 20, 2021
    Posts:
    11
    Hi, can you try adding the [DontDestroyOnLoad] to the script?
     
  3. levysn

    levysn

    Joined:
    Aug 10, 2023
    Posts:
    5
    I managed to solve the problems by loading each scene Singly one after the other using a List and a switch case. I'm still not quite sure why the unload didn't work in this case... but this other method has worked.