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:

Thanks for your help.

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:

  1. Create a singleton class to hold all the global data.
  2. Instantiate a GameObject and attach this class to it.
  3. Use “DontDestroyOnLoad()” method to persist this gameobject through different levels.