Hey everyone,
I'm loading a scene using SceneManager.LoadSceneAsync and it's working. Now, the scene that it's loading is huge, so I want to be able to quickly reload it if my character dies. Is there an easy way to do that?
I tried marking the object that I'm running LoadSceneAsync from as DontDestroyOnLoad, but I can't seem to reload the level from the same object that loaded the level in the first place.
Any ideas??
Thanks!
↧
One SceneManager.LoadSceneAsync across multiple scenes
↧
DontDestroyOnLoad Duplicating Objects When Scene Reloaded
I'm aware this is a generic issue, but i'm having trouble understanding.
When I reload my scene, I want to keep an object because it has previous time records. When the scene reloads, the object is kept, but another one is created because the scene has reloaded.
How do I prevent this?
↧
↧
Do not destroy is not working ?
Hey guys have do not destroy on a game object, the do not destroy thing comes up in my hirackey yet when the next scene loads with "PhotonNetwork.loadscene"
the Object disappears and gets deleted on scene load When I need the scene to stay in play.
nothing else delete this GameObject.
'`using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DoNotDestroy : MonoBehaviour {
void Awake() {
DontDestroyOnLoad(transform.gameObject);
}
}`
↧
Unity Reload Scene - References missing / objects inactive upon reload
When playing the active scene, all the references are linked through inspector like the game manager
![alt text][1]
However, when reaching a simple game over and pressed the restart button, all the references (like the game manager) are missing despite that all of the objects are tucked away under the same scene I am reloading.
![alt text][2]
I remember encountering a similar question about references, but the answer seems to be unclear except for DoNoDestroyOnLoad, which is not what I want to do. nor does it solve the problem of missing references.
Is there any way that I can reload the level **EXACTLY like the first time**? Without having to use a lot of "DoNotDestroyOnLoad".
Link to similar question without clear answer.
http://answers.unity3d.com/questions/1373722/keeping-reference-after-scene-reload.html
[1]: /storage/temp/101701-screen-shot-2017-09-11-at-32243-pm.png
[2]: /storage/temp/101702-screen-shot-2017-09-11-at-31823-pm.png
↧
DontDestroyOnLoad, where is my error?
Hello,
I'm new on Unity, so to start I'm on a little project of creating a hidden object games. I'm currently doing a prototype with the key feature. I find information (specially here) and manage to have invisible button and other stuff.
I now want to work some puzzle and other stuff that need to save data. So I'm creating, to start, a simple system: when I'm using a button to load a new scene, it adds one to a variable. Once I went twice on the same page, it's over and the end scene is automatically loaded. For that, I try to use DontDestroyOnLoad, but it seems that I am facing an issue I can't find. Here is the code, have you any idea?
The script is assigned to an empty object, and I have the expected messages on the console.
What is expected:
When I switch from the scene "cuisine" to the scene "chambre", I have a variable "chambre_count " that counts how many times I went to this scene (and there is the same system for cuisine with cuisine_count).
When I went twice on the scene "chambre", it loaded another scene "fin".
The issue:
cuisine_count and chambre_count are always rebooted when I change scene and back to 0. Which means the user is never going to the scene "fin", even if yes going 50 times on the scene chambre.
Here is the code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class Button_Behavior : MonoBehaviour {
private int cuisine_count;
private int chambre_count;
private bool initialized;
void Start()
{
}
void Awake()
{
Debug.Log("First Lol");
if (initialized)
{
Destroy(gameObject);
Debug.Log("If Lol");
}
else
{
GameObject.DontDestroyOnLoad(gameObject);
initialized = true;
Debug.Log("Else Lol");
}
Debug.Log("Last LOL");
}
public void ChangeScene(string sceneName)
{
Addtry(sceneName);
Application.LoadLevel(sceneName);
OverGame(sceneName);
}
void Addtry(string sceneName)
{
if (sceneName == "chambre")
{
chambre_count = chambre_count + 1;
print("Nombre de fois dans la chambre " + chambre_count);
}
else if (sceneName == "cuisine")
{
cuisine_count = cuisine_count + 1;
print("Nombre de fois dans la cuisine " + cuisine_count);
}
}
void OverGame(string sceneName)
{
if (chambre_count == 2)
{
sceneName = "fin";
ChangeScene(sceneName);
}
}
}
Cheers,
Mathieu
↧
↧
Need I put OnApplicationQuit into a DontDestroyOnLoad object?
I have some actions need to perform when the application is closed, like clearing some cached player preferences. No matter which scenes the user is, it should be executing.
Should I put it in a DontDestroyOnLoad object? or OnApplicationQuit is an event register that those code would run well even across the scenes?
Thank you.
↧
dontDestroyOnLoad doesn't work as expected with scene navigation
Hi,
I am a unity nab and I can't figure out this strange behaviour i have in my test game while using `dontDestroyOnLoad`.
I have a `__preloadScene` that is my very first scene that gets loaded in the game. It only has to load my `GameSparksManager`script inside an empty `GameObject` that needs to be a singletone that never gets destroyed.
My `GameSparksManager` singletone script is this:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class GameSparksManager : MonoBehaviour {
public static GameSparksManager instance = null;
void Start() {
Debug.Log ("ciao");
SceneManager.LoadScene("Auth Scene", LoadSceneMode.Additive);
}
private void Awake()
{
if(instance == null)
{
instance = this;
DontDestroyOnLoad(this.gameObject);
} else
{
Destroy(this.gameObject);
}
}
}
But what happen is that when you move between scenes the stuff inside them doesn't get destroyed, here an example video:
https://cl.ly/3B3o2X2K0H03
what am i doing wrong? thanks for your help
↧
Loading new Scene seems to break my DontDestroyOnLoad Variables
Hey guys, I've tried to research this for hours to find a solution but nothing seems to fix my problem. Maybe I am missing something that is staring me in the face... I'm not sure, my rubber ducky hasn't helped this time sadly :(
So I have a LevelManager, a SoundManager, and a GameManager. All of which are objects that are pre-placed in my splash screen scene. The next scene is called using Invoke and bring you to a new scene which is my main menu. I have the buttons set to talk to my GameManager script, who in turn passes the message onto the soundManager. At this point the extra step through my GameManager is a little unnecessary but I am trying to plan ahead with my scripting architecture. My problem is, even though the same instance of sound manager which I know because I tested with debug.log(GetInstanceID()). So I am confused as to why my variable returns null on the second scene? My music file plays just fine, but my button sound effect is returning null. Here's the code, I thought I understood how the scope worked and things but I noticed my button sound effect does not play UNLESS I re-declare it in PlayButtonClicked.
So I guess the part that has me confused is why does the function that plays my music know what to do on the next scene, but my sound effects function doesn't even though they are in the same class?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class GameManager : MonoBehaviour {
private LevelManager levelManager;
private SoundManager soundManager;
private GameManager instance = null;
void Start () {
levelManager = FindObjectOfType();
if (instance != null)
{
Destroy(gameObject);
}
else
{
instance = this;
GameObject.DontDestroyOnLoad(gameObject);
}
Invoke("LoadNewScene", 1f);
soundManager = FindObjectOfType().GetComponent();
}
void Update () {
}
public void LoadNewScene()
{
levelManager.LoadNextLevel();
soundManager.PlayMusic(SceneManager.GetActiveScene().buildIndex);
}
public void PlayButtonClicked()
{
Debug.Log("Calling Sound Manager....");
soundManager.ButtonSound();
}
}
Here's the second script...
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SoundManager : MonoBehaviour {
public AudioClip[] musicTrack;
public AudioClip buttonSFX;
private AudioSource[] audioSources;
private SoundManager instance = null;
AudioSource musicSource, sfxSource;
private void Awake()
{
audioSources = FindObjectsOfType();
foreach (AudioSource priorityCheck in audioSources)
if (priorityCheck.priority == 1)
{
musicSource = priorityCheck;
}
else
{
sfxSource = priorityCheck;
}
}
void Start()
{
if (instance != null)
{
Destroy(gameObject);
}
else
{
instance = this;
GameObject.DontDestroyOnLoad(gameObject);
}
}
public void PlayMusic (int level)
{
AudioClip thisLevelMusicTrack = musicTrack[level];
Debug.Log("Playing this level music track: " + thisLevelMusicTrack);
if (thisLevelMusicTrack)
{
musicSource.clip = thisLevelMusicTrack;
musicSource.loop = true;
musicSource.Play();
}
}
public void ButtonSound()
{
Debug.Log("Sound Manager Recieved the Call....");
sfxSource.clip = buttonSFX;
sfxSource.loop = false;
sfxSource.Play();
}
}
↧
HOW TO USE THE COMMAND DontDestroyOnLoad(); ?
i want to reset my game but when i use the command SceneManager.LoadScene, it resets all my scripts, variables,...
i want to keep something dont load with scene so i use dontdestroyonload but it still loads T_T
i dont know what i wronged, help me pls!!!. sorry my bad English
void Dontdestroyonload()
{
DontDestroyOnLoad(obj);
}
↧
↧
How to initiate a counter when the script runs once it increments and once called, increments again?
How to initiate a counter when the script runs once it increments and once called, increments again?
using UnityEngine;
using System.Collections;
public class LoadWorldMapCounter : MonoBehaviour {
public static LoadWorldMapCounter Instance;
public int LoadMapCount;
// Use this for initialization
public void Awake()
{
DontDestroyOnLoad (this);
Instance = this;
// Not sure where to put or how to use this so that everytime this script runs it adds 1-->LoadMapCount++;
}
public void Start()
{
// Not sure where to put or how to use this so that everytime this script runs it adds 1-->LoadMapCount++;
}
}
↧
"Dont destroy on load" does not work at forst
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class debugPlanePrefabController : MonoBehaviour {
// Use this for initialization
void Start () {
DontDestroyOnLoad (this);
}
// Update is called once per frame
void Update () {
}
}
This script was working normally in Unity but I noticed that this script did not work on my phone(Debug).
This gameobject destroyed.
What code should I add?
Or this problem is up to phone?
↧
how to disable dontdestroyonload on a specific scene
hey, i'm making a 2d game that contains bonus scenes, so i manage to put my score to go over the scenes and don't destroy, so it would have the same value when the player went back to the main scene. here's the problem: when the player dies, the score does not reset. how can it make it reset when the player is dead?
obs: the game over menu has a restart button and i tried to add something to his script, but it didn't work.
the restart button script: public class LoadSceneOnClick : MonoBehaviour {
public void LoadByIndex(int sceneIndex)
{
SceneManager.LoadScene (sceneIndex);
}
}
the dontdestroyonload script:
public class SaveScore : MonoBehaviour {
void Awake ()
{
DontDestroyOnLoad (this);
if (FindObjectsOfType (GetType ()).Length > 1)
{
Destroy (gameObject);
}
}
}
thanks in advance c:
↧
Do something at the end of every scene in a DontDestroyOnLoad object
I'm using Unity 5. I have an object with a bunch of scripts that is DontDestroyOnLoad. Is there a way to detect that the current scene is ending and to have the scripts on this object do something at the end of the scene? I have found some information on doing things at the start of the scene but I would like to do some cleanup before the scene ends.
I am also unclear if this solution will work for a DontDestroyOnLoad object entering a new scene.
https://forum.unity.com/threads/how-to-use-scenemanager-onsceneloaded.399221/
↧
↧
Canvas order and DontDestroyOnLoad
Hello! I have a problem with canvas order.
I have 3 scenes in game. There are canvases with own UI elements on each scene plus canvas with common UI elements which put into DontDestroyOnLoad and jorney between scenes.
All canvases has RenderMode=ScreenSpace-Overlay.
So when scene is loading I have it's own canvas at the top in hierarchy and canvas in DontDestroyOnLoad at the bottom. I cant't have access to the common UI elements in DontDestroyOnLoad canvas because it fully overlayed with top-in-hierarchy canvas belonged to current scene.
It seems that this problem wasn't in Unity 5.x because canvas at the bottom of hierarchy has highest priority. But it appear when I upgrade to 2017.x and it seems that now canvas at the top of hierarchy has highest priority.
Is there any ways to solve this problem?
↧
Use of DontDestroyonLoad()
I need to send a parameter from a scene to another, and I wanted to use DontDestroyonLoad().
But exactly, how can I take this parameter from the script attached to a button that I clicked to pass to next scene?
(I tried to use also static variables)
Script that contains DontDestroyOnLoad():
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Tunnel : MonoBehaviour
{
public string levelC;
// Use this for initialization
void Start()
{
DontDestroyOnLoad(this.gameObject.GetComponent());
}
public string LevelC
{
get { return levelC; }
}
}
Script that loads informations:
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class LoadInformations : MonoBehaviour {
public Text titolo;
public Text oro;
public Text argento;
public Text bronzo;
public Text extra;
private string filePath = "info.txt";
void Start () {
bool i = true;
string levelC = GameObject.Find("Tunnel").GetComponent().LevelC;
try
{
string line;
StreamReader sr = new StreamReader(filePath, Encoding.Default);
using (sr)
{
line = sr.ReadLine();
do
{
if (line != null)
{
string[] words = line.Split(',');
if (words.Length > 0) {
if (words[0]==levelC)
{
titolo.text = words[0]+" - "+words[1];
oro.text = words[2];
argento.text = words[3];
bronzo.text = words[4];
extra.text = words[5];
i = false;
}
}
}
line = sr.ReadLine();
}
while (line == null && i);
sr.Close();
}
}
catch (IOException e)
{
extra.text = "ERRORE!!! "+e;
}
}
}
↧
Losing reference to the camera after loading new scene
I am getting the error:> MissingReferenceException: The object of type 'Camera' has been destroyed but you are still trying to access it.Your script should either check if it is null or you should not destroy the object.
The camera in my game contains a persistent object script, which prevents the camera from being destroyed when loading a new scene.
private void Awake()
{
DontDestroyOnLoad(transform.gameObject);
}
The scene is switched via a method in the GameManager
public void loadScene(UnityEngine.Object arena)
{
operation = SceneManager.LoadSceneAsync(arena.name);
state = GameState.LOADING;
}
and finally the camera contains another script which adds a distortion effect
void OnRenderImage(RenderTexture source, RenderTexture destination)
{
distortCam.CopyFrom(mainCam); //Error occurs here
distortCam.backgroundColor = Color.grey;
// ...Some more code ...
}
The error occurs in this last method, but the camera still exists in the editor. mainCam is a Camera object that is assigned onAwake via `mainCam = GetComponent();`
Also if I switchthe loadScene method to
public void loadScene(UnityEngine.Object arena)
{
operation = SceneManager.LoadSceneAsync(arena.name, LoadSceneMode.Additive);
state = GameState.LOADING;
}
it works perfectly, but it is inconvenient to always have to loadScenes additive.
Why is the reference to the mainCam being lost? I have tried to check if it's null and reassigning it before using it, but so far I still have the problem. Any ideas?
↧
Don't destroy On Load make object not interactable in new scene
Hi
I'm having a small but annoying problem with DontDestroyOnLoad. I am loading my player into a new scene where it is supposed to interact with another object's collider in that scene. But its not doing that.
[the object in the new scene takes us back to the previous scene]
Could it be that since DontDestroyOnLoad puts the character in it own "scene" it makes it separate ? How do i fix that.
PS: The object that takes us back is not the problem. I tried the debug.log on the character whenever its supposed to interact with the object but nothing comes up in the console. All objects have a rigidbody and a trigger collider
↧
↧
Network Lobby Example from Asset Store doesn't work on SendReturnToLobby from Game Scene
When calling the SendReturnToLobby() method after the game ends, the Client's lobby is very messed up. It shows two copies of the Server player (only one of which is active), it doesn't allow me to change the color, and the server doesn't recognize that the client is in the lobby.
What could be wrong with this? Any help is greatly appreciated! Thanks
↧
Read variable from DontDestroyOnLoad GameObject C#
Hi I have a DontDestroyOnLoad GameObject that moves from Scene 1 to Scene 2.
I also have a GameObject in Scene 2 that has to read an int from the DontDestroyOnLoad Object.
Here is the Script DontDestroyOnLoad GameObject:
public int myInteger;
void Awake () {
DontDestroyOnLoad(gameObject);
}
void Start(){
myInteger = = Random.Range(1, 4);
}
My normal Method to read the Integer from my other GameObject would be something like:
public GameObject dontDestroyOnLoadObject;
void Update ()
{
if (dontDestroyOnLoadObject.GetComponent
↧
pass data between scene for game settings?
Ok, i've already read about passing data between scenes with gameobjects, components and Dontdestroyonload(), but what i mean here is : how can i handle this data to set up my gameplay in the loading scene?
If i'm switching from a lobby scene to a gameplay scene and i need to pass data/values about the players (like how many controllers/players are connected to spawn their characters) or about the environment (i don't really have any example on this but you got it now) or something else, and in the gameplay scene there is a gameobject responsible for spawning the characters etc, are those informations ready?
When the charachter-spawning gameobject is created and started in the gameplay scene can it immediately find the game settings gameobject from the previous scene and use its values to set all what it need?
Are there others ways to do this?
Thanks in advance to everybody.
↧