Hi all!
At the moment, I have a “GameManager”-Object that has a “DontDestoryOnLoad” script attached to it.
All the children of this “GameManager”-Object won’t re-spawn when exit and entering Scene 1…
However, when I Exit Scene 1 and destroy some crates on Scene 2, return to Scene 1 and then return back to Scene 2 - the crates have re-spawned. How do I assign these crates to my “GameManager”?
Isn’t there a convenient script that is “hovering” over all scenes and I can just drop all prefabs to this script that should remain “Destroyed” after they have been destroyed once…?
Scene 1
Sceene 2
My simple “DontDestroyOnLoad” Script that is attached to the “GameManager”
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DontDestroyOnLoad : MonoBehaviour {
public static DontDestroyOnLoad coincontrol;
private static bool GameManagerExists;
// Use this for initialization
void Start ()
{
if (!GameManagerExists) //if GameManagerexcistst is not true --> this action will happen.
{
GameManagerExists = true;
DontDestroyOnLoad(transform.gameObject); /// taken from this Tutorial https://youtu.be/x9lguwc0Pyk
}
else
{
Destroy(gameObject);
}
}
// Update is called once per frame
void Update () {
}
}
You can always have multiple scenes loaded at the same time. One scene could contain the crates, and one the main level. Load them at the same time. Then when you go to the other level, keep the crate scene loaded and set it as inactive. When you go back, just set it as active again. This way all changes in the crate level will remain.
I use this for keeping tabs on which coins have been collected in various levels, it works like a charm.
Hmm… What do you mean by loaded at the same time?
How do you go about to change that?
I have some objects i want to appear on Scene 2 (ex, you collect 10 coins and a Box appears on Scene 2):
But I don’t know how to interact with Scene 2 from scene 2 via script
I usually have a “root object” in the scene that is to persist, that is an empty object that contains everything in the scene. This makes it easy to deactivate everything in that scene.
Then you do something like this (this might need some work, but…)
// Global variable to keep the root object
GameObject boxSceneStuff;
// First time you load the scenes
SceneManager.LoadScene("LevelScene 1", LoadSceneMode.Additive);
SceneManager.LoadScene("BoxScene", LoadSceneMode.Additive);
boxSceneStuff = GameObject.Find("Root Object");
// When you go to the other scene
boxSceneStuff.SetActive(false);
SceneManager.UnloadSceneAsync("LevelScene 1");
SceneManager.LoadScene("LevelScene 2", LoadSceneMode.Additive);
// When you return to the first scene
boxSceneStuff.SetActive(true);
SceneManager.UnloadSceneAsync("LevelScene 2");
SceneManager.LoadScene("LevelScene 1", LoadSceneMode.Additive);
Ah… A bit tricky… I only have 1 thing I need to appear in one scene…
I thought about keeping this object invisible, but when I enter a certain scene it will collide with a BoxCollider and trigger “enable sprite renterer…” I will see if I can get around this problem with this. Thanks anyway!