- Home /
Destroying and Loading Object Promblem?
Hi there,
I'm trying to create a presentation with a simple interaction using buttons. I created a sample scene consisting of main menu, and two different scene with three buttons. The two buttons allows you to turn on/off the lights and a back button.
And the problem comes when I'm trying to switch from one scene to another, and back again to my first/second scene. All the buttons are not working telling that the object of type has been destroyed but you are still trying to access it. Could anyone fix my problem because I'm still learning c# in unity3d.
Here's the link of my sample file:
http://www.mediafire.com/?ec8dt0a6d5u5jbc
Thanks for your help.
Answer by appearance · Feb 06, 2013 at 11:57 AM
When you load a new scene using Application.LoadLevel(), all the gameobjects from current scene will be deleted. You cab use Application.LoadLevelAdditive() to load the new scene. This way your current level's objects will not be deleted.
This is a simple approach to solve your current issue. The efficient way to do could be:
Create a singleton class to hold all the global data.
Instantiate a GameObject and attach this class to it.
Use "DontDestroyOnLoad()" method to persist this gameobject through different levels.
Hi there, I've looked to the singleton class that your telling but I don't really have any idea on how to use it. Could you please show it to me ins$$anonymous$$d by using the sample file that I posted. If it is just ok with you, it would really be a great help for me. Thanks.
Create a class, name it as "Singleton.cs" and add the following code to it:
using UnityEngine;
using System.Collections;
public class Singleton : $$anonymous$$onoBehaviour {
private int temp_int;
public int TempInt { get { return temp_int; } set { temp_int = value; }}
private static Singleton _instance = null;
private Singleton () {}
public static Singleton Instance {
get {
if (_instance == null) {
_instance = GameObject.FindObjectOfType (typeof (Singleton)) as Singleton;
if (_instance == null) {
GameObject container = new GameObject ();
container.name = "__Singleton";
_instance = container.AddComponent <Singleton> ();
DontDestroyOnLoad (container);
_instance.Initialize ();
}
}
return _instance;
}
}
private void Initialize () {
// Do any initialization here...
TempInt = 10; // Only to show how to manage instance data members
}
public void OnApplicationQuit () {
Destroy (_instance);
_instance = null;
}
}
Now, create another class, name it as "UseSingleton.cs" and add the following code to it:
using UnityEngine;
using System.Collections;
public class UseSingleton : $$anonymous$$onoBehaviour {
// Use this for initialization
void Start () {
Singleton.Instance.TempInt = 20;
}
// Update is called once per frame
void Update () {
}
void OnGUI () {
if (GUI.Button (new Rect (0,0,100,20),"Temp UP")) {
Singleton.Instance.TempInt++;
}
GUI.Label (new Rect (105,0,100,20), Singleton.Instance.TempInt.ToString ());
}
}
Now you can attach 'UseSingleton' script to any gameobject in your hierarchy and run it to see how singleton can be created and used.
Your answer
Follow this Question
Related Questions
missing object woe beginner 1 Answer
A node in a childnode? 1 Answer
MissingReferenceException after destroying a prefab? 2 Answers
Destroy Istantiated objects after some time (different for each instance) 1 Answer
GUI activate and destroy 1 Answer