Search Unity

Hotswapping in Unity

Discussion in 'General Discussion' started by SarperS, Nov 19, 2012.

  1. SarperS

    SarperS

    Joined:
    Mar 10, 2009
    Posts:
    824
    I just realized that we can do hotswapping in Unity 4 now. Before this release, if I would alter the code in any way while the game was running in the editor, I would get a Null Reference exception, it's no longer the case. Can anyone validate this? If it's correct, why isn't it in the release notes?
     
  2. flaminghairball

    flaminghairball

    Joined:
    Jun 12, 2008
    Posts:
    868
    I don't recall this being an issue in Unity 3 either, with the exception of static variables (recompiling seemed to reset any static references). Does it no longer reset static references or am I missing something?
     
  3. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    15,620
    Recompiling in play mode has always broken things for me for a long while now. My current projects are still in Unity 3, though, haven't tried 4.
     
    dogzerx2 likes this.
  4. npsf3000

    npsf3000

    Joined:
    Sep 19, 2010
    Posts:
    3,830
    Would be interesting to know how hot swapping is meant to work - I usually consider it a bug because it breaks my running game without explanation. If I knew how it worked... then maybe it could be an asset.

    Just hot swapped some mouse look code and it worked in Unity4.
     
  5. lilymontoute

    lilymontoute

    Joined:
    Feb 8, 2011
    Posts:
    1,181
    From what I understand, it's mostly the same as before.

    When code is recompiled, everything is serialized, Mono is re-initialized, and then serialized values are pulled back in. Only things that are serialized into C++ land and then back are saved (public inspector variables, [SerializeField] stuff that Unity supports, ScriptableObjects, etc. So static variables will be re-initialized, private non-serialized variables reinitialized, etc.

    There was a blog post about Unity's serialization made not too long ago - if you design your code right, you can actually have hot swapping working (It's pretty tricky though and sometimes not really possible)
     
    angrypenguin likes this.
  6. wesley-zeng

    wesley-zeng

    Joined:
    Sep 17, 2014
    Posts:
    1
    hotswapping is possible in unity.
    we use call c sharp code in lua and hotswap lua script from web server.
    search bblua + bbupdate in assetstore.
     
  7. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    21,203
    Well this thread is over two years old...
     
  8. Gerold_Meisinger

    Gerold_Meisinger

    Joined:
    Sep 8, 2015
    Posts:
    93
    KyryloKuzyk and murgonen like this.
  9. Schneider21

    Schneider21

    Joined:
    Feb 6, 2014
    Posts:
    3,512
    I blame Google for all these necro instances. It seems that recently search results include at the top articles and posts from long ago, where as before it would show more recent results first.

    Then again, people could check to see when a thread was posted or last responded to before replying... :p
     
  10. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    21,203
    It's amazing though how most of the necro instances are usually one liners.
     
    Schneider21 likes this.