- Home /
How Much code can be put in a start function?
ok..... so i have a code that check if the player is in the right scene and depending on the scene the player will be able to change between two color using the arrow keys....
LIKE, scene 1: the player will be able to change from color black to white
scene 2: the player will be able to change from blue to white.
scene 3 the player will be able to change from yellow and white.. and so on..
right now the code for that requirement is in the update function, I was wondering if it would be best if I put it in the Start function as I just need the player can change between two color to depending on the scene and this can be done only once I presume.. And doing that in update function might create performance issue because it is being run through every frame for no reason when it would be more logical to run it one time.
Because the code for changing color depending on the scene is LONG I was wondering if including the code in the Start function will create any issues.. And if there is a more efficient way of doing that requirement.
Thank you for helping.. :)
Answer by Eno-Khaon · Apr 27, 2018 at 09:13 PM
If you run 1 billion lines of code once, you wait as long as it takes to process 1 billion lines of code one time. If you run 1 billion lines of code every frame, you wait for that long every frame.
It would make perfect sense to make this sort of decision once (per scene), such as using the Start() function. All you really need to do is have your colorA and colorB set aside, then assign to them:
// C# based, lots of pseudo-code
Color colorA;
Color colorB;
void Start()
{
switch(currentScene)
{
case 1:
colorA = Color.black;
colorB = Color.white;
break;
case 2:
colorA = Color.blue;
colorB = Color.white;
break;
case 3:
colorA = Color.yellow;
colorB = Color.white;
break;
// etc.
}
}
Then, in Update(), you simply assign colorA or colorB based on the input, rather than performing any further logic at that time.
ohhh.... Thank you mate ... I thought it would lag or something else..
Thanks again..
Your answer
Follow this Question
Related Questions
One big script or lots of small ones? 0 Answers
Text Update demands too much performance 0 Answers
So what's Mesh.UploadMeshData do exactly? 3 Answers
How optimize script 4 Answers
Loading Cards with different langauges 0 Answers