- Home /
"Caching" code in the Start() to run it "later"
I have a lot of code in the start function of my script and all of it is essential for the game to work properly, and when I run it on my phone, it lags for a second or 2 until it's done. Isn't it better to cache all that code in a list and run each line every 10th frame or something and have a small loading screen instead?
I think you may be able to fix this by just having a loading screen if you aren't using one. I believe the loading screen will display until the Start() functions are complete?
Not sure if it's worth getting too fancy with Start() but then again I don't know anything about your project.
Two questions that come to $$anonymous$$d are:
Is the same work being done by several instances of the same script? Could they share the results?
Can some of this work be performed with Lazy Initialization?
A loading screen could probably work, so it waits like 2 seconds before removing it and starting the game...
It's all being done by the same script, it adds all the ground objects in the game(2D game) to a list, and changes some color on objects.
What's lazy initialization?
Lazy initialization is technique to put off work/research that may not be needed until you know that it is, here is one example:
private GameObject[] _players[];
public GameObject[] Players {
get {
if (_players == null) {
_players = FindGameObjectsWithTag("Players");
}
return _players ;
}
}
// Then your code can access Players knowing that it will always return a valid array.
When used appropriately putting of this work until it is actually needed will spread it over several frames so the player will not experience any upfront delay.
It doesn't look like it would be a good fit in your case. A loading screen sounds easiest, and/or some optimisation might be possible?
I wouldn't have a time based loading screen, dismiss it when your work is done and the level is ready to play.
Also be aware that coroutines are effected by the timescale.
Answer by komodor · Jan 30, 2014 at 10:38 AM
maybe you need to put some code to Awake, because Start is the first frame of GameObject (the enable exactly), while Awake is something like onLoad
here is the list of all methods http://docs.unity3d.com/Documentation/ScriptReference/MonoBehaviour.html
if you need to distribute some code for later, you can use coroutines http://docs.unity3d.com/Documentation/Manual/Coroutines.html or you can just use Update function to call it later
Thanks, do I can pause the game in awake and run all my code there, and then un-pause it in start.
Answer by Nanocentury · Jan 28, 2014 at 11:02 PM
This can be achieved with Co-Routines: http://unity3d.com/learn/tutorials/modules/intermediate/scripting/coroutines
Answer by DynamicPrgm · Jan 28, 2014 at 11:56 PM
You could use another function?
void DoThis {
//code
}
void Start() {
DoThis();
}
bool youneedthisnow;
void Update() {
youneedthisnow = true;
if(youneedthisnow){
DoThis();
}
}
How does this change / optimise anything? O.o
Also what's that for an if statement outside of a function? That wouldn't compile.
I don't know, I was just suggesting a possible solution. I know this won't compile, I'll fix it now. It was just an example :P
Your answer
Follow this Question
Related Questions
Initialising List array for use in a custom Editor 1 Answer
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Save and Load Prefab and Mesh 1 Answer