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 zz74b · May 04, 2012 at 08:37 AM · c#dontdestroyonloadinterfaces

Dontdestroyonload and interfaces

Hi, please consider the following: (all in c#)

interfaces:

 public interface ITypeA 
 {
     ITypeB { get; set; }
 }
 public interface ITypeB { }

container class:

 public class OuterContainer : MonoBehaviour, ITypeA
 {
     void Awake()
     {
         if (InnerObjectB == null) InnerObjectB = AddComponent<InnerClass>();
     }

     ITypeB InnerObjectB { get; set; }
 }

inner class:

 public class InnerClass : MonoBehaviour, ITypeB
 {
     // Some implementation
 }

script:

 private OuterContainer storage;

 void Awake()
 {
     DontDestroyOnLoad(this);
     var component = AddComponent<OuterContainer >();
     storage = component;
 }


What I am seeing happen is the following:

The normal MonoBehaviour methods (Awake, Start, Update etc) are called on all objects (the script, Outer container and inner class) as normal in the first scene. Then when the scene is changed using LoadLevel although the script still is called as normal, the 'InnerClass' object no longer receives any MonoBehaviour calls (although it does exist!)

Can anybody shed any light on why this might be happening?

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
2
Best Answer

Answer by amirabiri · May 04, 2012 at 10:55 AM

I don't fully understand your context, but try DontDestroyOnLoad(gameobject) instead of DontDestroyOnLoad(this). In a component "this" refers to the component, not the game object. According to the documentation it shouldn't matter, but docs can lie.

Check if the behaviours are still present on the game object in the editor by pausing the game after a level load and inspecting the objects in the inspector.

Objects can still exist in the C# VM after they have been "destroyed" by Unity because Unity. Destroying an object in Unity only means that it is not part of the running scene and Unity doesn't hold any references to it anymore. But the object might not have been GC'ed yet and if your in-scene objects still hold a reference to it it won't get GC'ed.

Comment
Add comment · Show 1 · 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 zz74b · May 10, 2012 at 10:50 AM 0
Share

I'm rather embarrassed about this, but it turns out that I had a Destroy(this) call in the code that was disposing of the instance.

The confusing thing was that the reference wasn't yet null, hence me thinking it was valid, but calling any actions on it yielded an error about using destroyed objects. Evidently when you Destroy and object unity ceases to send Awake/Update etc calls to it.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

How do I save the high score from the game scene onto the main menu scene using PlayerPrefs and Dontdestroyonload? 3 Answers

PlayerPrefs to save setActive state 2 Answers

Play again music problem. (Please help) :) 0 Answers


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