Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by rayhel · Feb 06, 2013 at 09:59 AM · destroyonguimissingreferenceexceptionresource.load

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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:

  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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image rayhel · Feb 06, 2013 at 01:31 PM 0
Share

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.

avatar image appearance · Feb 07, 2013 at 06:39 AM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

10 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges