Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

Question Please help me with the following : It's urgent

Discussion in 'Scripting' started by newr72909, May 26, 2024.

  1. newr72909

    newr72909

    Joined:
    Jan 14, 2024
    Posts:
    10
    1st need
    How to run scene in background while other scene is being displayed?
    I mean if a timer is in another scene it continues running when we are in another scene
    2nd need
    I tried this code :
    Code (CSharp):
    1.     public void factory()
    2.     {
    3.         SceneManager.LoadScene(1,LoadSceneMode.Additive);
    4.     }    
    5.        
    6.     public void mainui_()
    7.     {
    8.         SceneManager.UnloadScene(1);
    9.  
    10.     }


    It indeed fulfils my requirements but i don't know how to hide additive scene in the current running scene.
    3rd need
    how to transfer data between scenes?
    I am sounding a bit silly. I'm struggling for a night to do this yet no result of my need.
    Please help me guys I have only few hours to implement this in my game for a game jam help me guys
     
  2. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    6,922
    Not possible.

    Set the game object with the timer component as "Don't Destroy on Load".
    Or just record the start time (see Time class), then check if current time minus start time is greater than the threshold.

    Have all that content in the scene in the same root object. Set the root object active or inactive as needed.

    Don't Destroy on Load, serialize & deserialize, singleton or static class / fields.

    Or quite simply: don't change scenes. A lot of times scenes are being used to show new content but all of that could be in either additive scenes or within the same scene, with merely the respective game objects set active/inactive as needed.
     
  3. newr72909

    newr72909

    Joined:
    Jan 14, 2024
    Posts:
    10
    Actually it is possible.
    Here's the logic
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.SceneManagement;
    3.  
    4. public class simplescenecontroller : MonoBehaviour
    5. {
    6.     public void LoadSceneAdditively(string sceneName)
    7.     {
    8.         SceneManager.LoadSceneAsync(sceneName, LoadSceneMode.Additive).completed += (AsyncOperation op) =>
    9.         {
    10.             ToggleSceneVisibility(sceneName, false); // Hide the scene initially
    11.         };
    12.     }
    13.  
    14.     public void ToggleSceneVisibility(string sceneName, bool isVisible)
    15.     {
    16.         Scene scene = SceneManager.GetSceneByName(sceneName);
    17.         if (scene.IsValid())
    18.         {
    19.             foreach (GameObject rootObject in scene.GetRootGameObjects())
    20.             {
    21.                 SetGameObjectVisibility(rootObject, isVisible);
    22.             }
    23.         }
    24.     }
    25.  
    26.     private void SetGameObjectVisibility(GameObject obj, bool isVisible)
    27.     {
    28.         // Hide or show renderers only, keep other components active
    29.         foreach (var renderer in obj.GetComponentsInChildren<Renderer>())
    30.         {
    31.             renderer.enabled = isVisible;
    32.         }
    33.  
    34.         foreach (var canvas in obj.GetComponentsInChildren<Canvas>())
    35.         {
    36.             canvas.enabled = isVisible;
    37.         }
    38.     }
    39. }
    40. //example
    41.         if (Input.GetKeyDown(KeyCode.Alpha1))
    42.         {
    43.             // Hide the additive scene visually
    44.             sceneController.ToggleSceneVisibility("factory scene", false);
    45.             sceneController.ToggleSceneVisibility("MainGame",true);
    46.  
    47.         }
    48.         if (Input.GetKeyDown(KeyCode.Alpha2))
    49.         {
    50.             // Show the additive scene visually
    51.             sceneController.ToggleSceneVisibility("factory scene", true);
    52.             sceneController.ToggleSceneVisibility("MainGame",false);
    53.  
    54.         }
    55. //ignore the scene names
    56.  
     
  4. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    6,922
    Additive scenes are effectively a single scene with additional content loaded in.
    It's not possible with single-scene loading is what I meant. ;)
     
    newr72909 likes this.
  5. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    21,687
    ChatGPT (specifically GPT-4 and GPT-4o) is great for learning how to do simple tasks like this one on a very tight deadline. Just be sure to tell it to be minimal with the code that it shows and only show code. Here's the code that it came up with when asked how to show/hide an additive scene.

    Code (csharp):
    1. using UnityEngine;
    2. using UnityEngine.SceneManagement;
    3.  
    4. public class HideAdditiveScene : MonoBehaviour
    5. {
    6.     public string additiveSceneName;
    7.  
    8.     void Start()
    9.     {
    10.         Scene additiveScene = SceneManager.GetSceneByName(additiveSceneName);
    11.         if (additiveScene.IsValid())
    12.         {
    13.             SetSceneActive(additiveScene, false);
    14.         }
    15.     }
    16.  
    17.     void SetSceneActive(Scene scene, bool isActive)
    18.     {
    19.         GameObject[] rootObjects = scene.GetRootGameObjects();
    20.         foreach (GameObject obj in rootObjects)
    21.         {
    22.             obj.SetActive(isActive);
    23.         }
    24.     }
    25. }
     
    Last edited: May 26, 2024