So I have a gameobject which loads new scenes when nessesarry. The script attached to the game object has a variable which holds the reference to a canvas element(the canvas element has a progressbar). The problem is that after I load a new scene and I want to load a new scene in that loaded scene I lose the reference to the canvas object because I can't set him to DontDestroyOnLoad.
What I have done is I put the canvas element (see commented line in start function()) as a child of the gameobject which doesnt get destroyed.. After I have done that i dont get the warning that I have lost the object reference BUT it wont enable the canvas in the setNewNameAndLoadScene() function.
EDIT: During the runtime i have put my canvas in the new loaded scene canvas it has shown the cav. but the problem is it only works if its in the scene canvas. It does not help if I enable it. So the problem is if I make my cav a child of the new scene canvas. I lose again my reference as it is not longer attached to my gameobject.
Here is the code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class SceneLoaderGameObject : MonoBehaviour {
private SceneLoader sl;
public Canvas cav; //loses reference when loading new scene
void Start () {
sl = new SceneLoader ();
cav.GetComponent
↧
Canvas will only appear on the game screen when its child of a new loaded scene canvas
↧
One SceneManager.LoadSceneAsync across multiple scenes
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!
↧
↧
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
↧
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?
↧
DontDestroyOnLoad for a specific scene
I have a scene called 'game'. And a gameobject. Every time the player selects the restart button, the game scene loads again. I don't want the gameobject to destroy when the game scene is loaded fresh. But when the player switches to the menu scene I want the gameobject to destroy. How can I do this? Thank you.
↧
DontDestroyOnLoad Error with Firebase Analytics
Hi. I am trying to export a build to Android, but I get the following error:
Error building Player: InvalidOperationException: The following game object is invoking the DontDestroyOnLoad method: Firebase Services. Notice that DontDestroyOnLoad can only be used in play mode and, as such, cannot be part of an editor script.
I am using Firebase Analytics. I have tried cleaning the project of anything related to Firebase and reimported the newest version, but with no luck. Any ideas?
↧
Objects remain under DontDestroyOnLoad despite being removed from the parent
I'm trying to upgrade a project from Unity 4.6 to 5.4 and I'm having an issue with objects not being removed when loading a new scene.
I have a root game object which contains the player, camera, game manager, etc. and I use DontDestroyOnLoad to keep it when loading scenes.
The player is able to pick up and drop/throw objects, which I do by parenting/unparenting the object to the player.
But when I set an object's parent to null, the object stays under DontDestroyOnLoad in the hierarchy, so they remain after loading the next scene.
I can see that Unity 5.4 shows the scene and DontDestroyOnLoad as two root items in the hierarchy, unlike 4.6. Is there a way to detach the child objects from the player and remove them from DontDestroyOnLoad?
Do I actually need to parent them to some other root object?
↧
↧
Question Regarding Don'tDestroyOnLoad Object
Hi, I want to ask one thing, I have some objects that attached with the script of Don'tDestroyOnLoad scripts so that the objects will brought to other scene... But I want a certain scene to not have that don'tdestroyonload objects, so i have been searching and found the code working but the problem is once I return to that certain scene all the button at there won't work anymore like being stuck or being trampled by invisible panel so i can't touch anything...
Here the code for one of my objects
public class CameraCon : MonoBehaviour {
public Transform target;
public float mspeed;
Camera mycam;
private static bool camExists;
public static int menuScreenBuildIndex;
// Use this for initialization
void Start () {
DontDestroyOnLoad (transform.gameObject);
mycam = GetComponent ();
if(!camExists)
{
camExists = true;
DontDestroyOnLoad (transform.gameObject);
} else {
Destroy (gameObject);
}
SceneManager.activeSceneChanged += DestroyOnMenuScreen;
}
// Update is called once per frame
void Update () {
if (target){
transform.position = Vector3.Lerp (transform.position, target.position, mspeed) + new Vector3 (0, 0, -10);
}
}
void DestroyOnMenuScreen(Scene oldScene, Scene newScene)
{
if (newScene.buildIndex == menuScreenBuildIndex)
{
Destroy (gameObject);
}
}
}
I hope everyone can give me a solution on how to destroy the objects on certain scene
For Example Case : I made an 2D RPG game with some objects that i don't want to destroyonload but I want to destroy it when I return to the main menu from pause and when I back to my game again, I want everything back to normal again like usual... (Probably might include saving or loading system that i still don't understand so much)
↧
GameObject::scene and DontDestroyOnLoad()
Hi,
If I assign a `GameObject` to the `DontDestroyOnLoad()` method and then call `print(gameObject.scene.name)` it prints out *"DontDestroyOnLoad"* instead of the scene name. Is there a way to get the scene in which the object was created after assigning it to `DontDestroyOnLoad()`?
I know it can be done prior but I'm looking for a way to check after without storing a variable.
Thanks,
↧
How do you transfer UI Text from one scene to another?
The scoring of my game is based on how far the player travels on the x-axis. I use this code to calculate it:
public Transform player;
public Text scoreText;
void Update()
{
scoreText.text = player.position.x.ToString("0");
}
After the player collides with an object, they are sent to the *Game Over* scene. The problem is, I am not able to/ I don't know how to display the final score when they lose. I have tried using DontDestroyOnLoad and PlayerPrefs, but I couldn't get either of them to work with UI Text.
Can someone help me?
↧