Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

MonoBehaviour losing member data before event listener fires

Discussion in 'Scripting' started by Nick-Wiggill, Feb 26, 2010.

  1. Nick-Wiggill

    Nick-Wiggill

    Joined:
    Jan 31, 2010
    Posts:
    46
    I have a script/class Initialise placed on an empty GameObject in the world. It extends MonoBehaviour.

    On execution, Initialise creates an instance of class Graph (non-monobehaviour), and sets up some event listeners (delegate/event style) to listen to that Graph instance. Initialise calls a method on Graph that causes an event to be fired from Graph right away, and before that event can be received asynchronously, Initialise terminates.

    Graph's event then reaches Initialise, whose delegate method is still listening. This is all fine and good, but it appears that Initialise (being a MonoBehaviour) has already been GC'ed by this time -- so any attempts to access it's members result in reset values or null references.

    This is very frustrating. Is there a standard way to deal with this (I read something about ScriptableObject but I'm not sure if that's right) or is my way of going about this all wrong for use with Unity? :eek:
     
  2. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    monobehaviours are not terminated unless:

    1. it is destroyed
    2. The game object on which it is is destroyed
    3. The whole scene is destroyed

    :)

    so if none of that holds, something else is the cause.
    If any of that holds, your code need redesign.
     
  3. Nick-Wiggill

    Nick-Wiggill

    Joined:
    Jan 31, 2010
    Posts:
    46
    Nice catch -- you helped me eliminate that possibility so I could focus on the real problem -- a couple of lines were switched around ;)
     
  4. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    its always the trivial stuff one misses :)
     
  5. Nick-Wiggill

    Nick-Wiggill

    Joined:
    Jan 31, 2010
    Posts:
    46
    :D Aye...

    By the way, re ScriptableObject, if I understand correctly it allows me to run scripts without creating an empty GameObject to attach them to.

    I'm used to the AS3 paradigm where you have a document class, or C++ paradigm where you specify main() to start up your application. Is there anything like that Unity?

    The problem I see with ScriptableObject is that whatever inherits from it still needs to be instantiated by a script on another GameObject, is that right?
     
  6. andeeeee

    andeeeee

    Joined:
    Jul 19, 2005
    Posts:
    8,768
    There is no global script that represents either the game or the scene in Unity. You can add empty GameObjects to the scene to apply general setup or supervising code.
     
  7. Nick-Wiggill

    Nick-Wiggill

    Joined:
    Jan 31, 2010
    Posts:
    46
    sup Andeeee,

    Okay -- so what is ScriptableObject actually for then? This is doing my head in -- I must know!

    ;)
     
  8. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    scriptableobject is kind of the base upon which the monobehavior builds, but without the update callbacks etc. it only enables and disables and naturally its serialized