- Home /
Unity Editor launches game much more slowly after 2nd time
Have a weird problem that has shown up recently, my game will launch fine if upon my first starting up Unity, but the 2nd time and subsequent times, it will take 5 to 10 times longer to launch. From what I can tell only when I close unity and restart it will the game launch as quick as it used to, but then again, only the first time.
Also, and maybe related, I have a bad bad lag between Mono and Unity, especially if I change a script. This lag seems around the same length of lag I am getting with the game launch.
One thing I notice is when I am in unity my 'warnings' are around a dozen, but when I go to Mono, change a script, press save and return to Unity, I cannot touch anything for a while(that lag mentioned above), and when it does return to where I can do things, the warnings shoot up to 4x that, with many warnings I swear I have not seen before but in scripts that have been around for a while(mostly private variables that are not given a value until it hits a if).
Any ideas?
One thing of note, is the game runs fine when I build it for pc, no launch lag no matter how many times I run it.
how many warnings do you have... what are they? for the most part.. you should fix warnings(e.g initializing variables). it sounds like the re compilers is slowing you down.. when you launch a game the compiler will go through all of your scripts for changes and compile them accordingly(it does it after test plays too on my version )... that's why there is lag between changes of scripts and Test Play(and that's of equal length)...
this should only appear in the EDITOR... when you release the game... it will not exist since the game is already compiled.
EDIT: just read your comment again... the compiler doesn't exist on builds(PC or Android or Apple doesn't matter).. so it can't cause lag
if there's a lot of warnings that could be slowing you down(like 700 or more)
Cleaned out the warnings (there were like 40 of them) and it's still the same delay/lag :(
Added info, when I have legit errors there is no lag from $$anonymous$$ono to Unity, but the $$anonymous$$ute I press 'save' in $$anonymous$$ono after fixing any actual error, the lag will be there in getting back to Unity and in launching the program.
Further added info, hopefully this will help.
I thought it might be my system, but all of my other projects do not show this symptom, they load up speedily every time.
If I change projects to another and back to this project, it will load up fine, but again, only for the first time, then I get the same 15 second or so lag(where it loads in 2 or so seconds when I restart unity or the project).
Answer by primemover · Oct 21, 2014 at 07:14 AM
Thanks for the responses guys, I figured out what was causing the delay, I had a script that had a public class that had [Serializable] on it, once I took that out everything went back to normal.
I an attempt to save game-state data in a binary format, I created a serializable non-monobehavior based public class that mimicked a regular monobehavior based class, had the same properties and all that, so I can serialize them in a binary format for saving the game-state. I am obviously using it incorrectly though I was able to get my data to save, so it's kind of disappointing, I have to read up more on how to do this correctly.
FURTHER INFO: It wasn't actually the existence of the serialized class itself, it's that I was making a new instance of it every time I created a new gameobject item. Once I take out that instance creation, it's all good.
a serialization loop? never thought of that... glad you got your self back up and running again :)...
I always get a chill down my spine when I read "save data in binary". Can I ask you to explain why you would need to mimick $$anonymous$$onoBehaviour to do this?
I am using C#'s binary formatter to serialize and save data for later loading and saving, and from what I know and experienced, you can't serialize a monobehavior. But, this is from my limited experience, so I am most likely doing the wrong thing or being misled.
Before this I was saving my gamestate and worldstate(it's a editable world) data in text format, this being not so smart I looked into other ways, and I read about saving as a binary via this method. I got it all to work for the worldstate, saving and loading and it was 3x smaller of a save and loaded and saved like 10x faster. I just got in trouble when I tried to make a class for my items, and tried to serialize it.
Answer by jtok4j · Oct 21, 2014 at 06:57 AM
Greetings Primemover,
I've been using Unity for about a year now, and the behaviour you're describing is normal, as far as I can tell.
In order for the editor to maintain a proper handle on things, it's got to be 100% sure that the scripts and such which are used at the "Play" (testing the game) launch, are not full of errors or it'll crash.
Thus, when you make a change to a script in Mono (And for me too, and I believe, everyone else) the Unity editor actually reviews the changes and (Perhaps all other scripts currently being used in the scene) and then let's you know if there's errors or warnings in the Console area. (During this time, look down at the very bottom right of the corner of the editor, and you'll see there is moving wheel/progress kind of indicator, showing that the editor is busy scanning your scripts and putting the input into the Console, if there's anything it needs to tell you about...
So, to be honest, it's really annoying, when you're simply changing a value to tweak it, and test it's performance in-game, but I believe that this is actually normal Unity engine behaviour...
Also, I've noticed that Unity doesn't actually start performing these script checks, until you change the "focus" from Mono, back to the Unity Engine. (So, you can change a script and save it, but Unity doesn't pick this up, until you switch from Mono back to the Unity engine. So, you could change a script, save it, walk away from your computer, and then return 15 minutes later, then click on the Unity window, and only "then" will it check the scripts, and you'll experience the delay while it does the checks... really annoying, I know... )
On my last (paid! Hurray!) project, I was totally sick of making adjustments to scripts, 'cause I knew I'd have to wait, after every little tweak and save... just to try it out in testing and see if I had got it right.
(Unity Community! Please correct me if I'm a little off/wrong in this functionality. Actually I'd LOVE (& pay for the possibility, too) to not have to wait every time that I change a script and save it...
Well a benefit from using UnityScript is that C# scripts dont have to recompile when you make changes in the JS file. Also having scripts in a DLL reduces the wait time from when you save the file. As those files dont need to be rebuilt.
Oh wow, Linus! Thanks for these tips. I'm gonna have to try this out, and see if I can speed up my dev work a bit. :D
Thanks for this response jtok4j, I appreciate the time taken. The issue turned out to be something completely different as I posted in an answer.
Your answer
Follow this Question
Related Questions
iOS : Delayed response to touch began? 1 Answer
linerenderer lags behind object positions 4 Answers
How to prevent input lag/delay? 1 Answer
Mouse event build up? 0 Answers
What causes long delay after hitting run in editor, how to reduce it? 0 Answers