Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

HELP! I have a really bad problem!

Discussion in 'Scripting' started by FuzzyQuills, Sep 9, 2014.

  1. FuzzyQuills

    FuzzyQuills

    Joined:
    Jun 8, 2013
    Posts:
    2,871
    HI guys. I may have to report to Unity HQ about a bug. Everytime I return to main menu in my game, everything seems fine, until I go and click play; what it should do is reload the level from scratch, but instead, when the level loads, the old positions of the cars, scores... almost EVERYTHING that was happening in the scene before the scene was supposedly "Destroyed" from memory leaks into the freshly loaded level! and I DO NOT have DontDestroyOnLoad activated in any of my scripts!

    So does someone know of a function that can clean out the level loading memory? or is this a HUGE bug in Unity that has some headache workaround?

    EDIT: Also looked around in the docs, and it says an object doesn't survive the wipe when loading a new level. but ALL cars are at their previous positions when re-loading from the main menu! How is that called "unrecoverable"?!
     
  2. GarthSmith

    GarthSmith

    Joined:
    Apr 26, 2012
    Posts:
    1,240
    How are you loading the new scene? Can you post some code?
     
  3. Graph

    Graph

    Joined:
    Jun 8, 2014
    Posts:
    153
    had the same problem, used destroyimmediately before the level load
    not on my "god object" but on my scene contained manager object
     
  4. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    My moneys on statics
     
  5. FuzzyQuills

    FuzzyQuills

    Joined:
    Jun 8, 2013
    Posts:
    2,871
    I did find a workaround: since I have to eventually instantiate cars at runtime, for car-selection reasons, (and an excuse to give the CPUs stupid names... :D) I simply destroy everything when the "god object" sends a onLevelLoadStart type of message, of which doesn't exist by default. Maybe UT should integrate such an event for garbage collection? :)

    Oh, and after putting the manual garbage collection in, the game now loads tracks properly. nice ideas of how to do it though! :)
     
  6. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,840
    You mean like this one?

    FWIW, I've never seen LoadLevel fail to destroy all the game objects that I hadn't specifically told DontDestroyOnLoad. Like hpjohn, my money is also on static variables.
     
  7. FuzzyQuills

    FuzzyQuills

    Joined:
    Jun 8, 2013
    Posts:
    2,871
    Yes, only I send the message before I call LoadLevel, to tell all receiving objects to destroy themselves before the level loads. this cleans up the cars, and resets the track for next time.

    I also ask, What kind of game cleans itself up properly regardless of non-static variables on your end? mine's a racing game, yours might be different... ;) I will also say one thing, and that is, it doesn't happen on one of my other projects, of which is a multiplayer shooter. (Well, except for an overlapping UI, of which is caused by TWO loading objects! I can easily fix this mishap though... :D)

    And point being, for a lot of global things, i use static variables. and that's easy to reset: just re-initialize the default value before exiting! :) BTW, what's your avatar? looks rather cute! :)

    EDIT: BTW, I am sure all objects destroy themselves, but for some reason, parts of the objects still remain in memory. that's why I have to destroy the objects manually.

    I also have a new problem: after doing the manual garbage collection, my AI cars, every now and then, will get stuck in part of the track, (will improve once I get a proper track design in...) then all of a sudden, the part that steers the car gives the transform rotation an NaN value, causing it to lag BADLY on android!

    the error:
    !CompareApproximately(sqr_magnitude) (I think this is caused by Mathf.Lerp())
    the transform rotation assign attempt to <name of car object> is invalid. input rotation is {NaN, NaN, NaN, NaN} (this is where the tablet starts lagging! I also found the cars don't respawn properly after this hapens... ;))

    oh, and this happens even if I don't realod the level.
     
    Last edited: Sep 12, 2014
  8. FuzzyQuills

    FuzzyQuills

    Joined:
    Jun 8, 2013
    Posts:
    2,871
    Also, is your game, "High Frontier" (Caught my attention when posting... :D) using the new UI? or did you find a clever trick to get those buttons looking nice? :)
     
  9. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    UnityGUI can be skinned to look like whatever you want. Alternatively, mb a custom made ui solution, there are no tricks
     
  10. FuzzyQuills

    FuzzyQuills

    Joined:
    Jun 8, 2013
    Posts:
    2,871
    @hpjohn: well, yes, I am well aware of GUI skins, but to get buttons like JoeStrout did takes a bit of work, especially when the button's an image that stretches! Perhaps there's a parameter to stop the stretching on parts of the button? Something tells me there is... :D
     
  11. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,840
    Yes, High Frontier uses the (current) standard GUI scripting, e.g. GUI.Button and so on. To get proper resizing images, you use the "Border" property in the GUISkin styles to define what part of the buttons should not stretch.

    And Fuzzy, my avatar is a lemming from an old Far Side cartoon, which I traced, cleaned, and colored in InkPaint.

    Oh yeah, and NaNs do throw a serious monkey wrench into the works... especially since they tend to reproduce and propagate. You need to work backwards to track down the source of the very first NaN, and fix that; chances are good they'll all go away after that.
     
  12. FuzzyQuills

    FuzzyQuills

    Joined:
    Jun 8, 2013
    Posts:
    2,871
    @JoeStrout: I did find the cause of the NaN: setting both the y eulerAngle and freezing the angularVelocity of the rigidbody when it flew off the ground threw the error. (Actually, the car got stuck on a curb before the error was thrown...)

    And I thought there was a use for the "Border" in GUISkin! so thanks for pointing this out. :) And far Side... I've heard it before, but I have no idea why I don't know what it is! :D