is it more optimal to use 2 short scripts or 1 long script?
is it more optimal to use 2 short scripts or 1 long script?
does it make any difference and if so how can you prove this?
please only respond if your sure
so for the player is it more optimal to have 2 scripts short or a longer one or does it make no difference?
2d game
Depends heavily on how you communicate between the two scripts. If you do it efficiently by reusing an existing reference the difference would be negligible. Every time you use Vector3.zero
is equivalent to calling a method in another script. Having one long script makes it less reusable though.
thanks but im a little baffled by your wording less reusable?? is that good or bad
also what do you mean communicate between the 2 scipts? they don't communicate with eachother they are separate fucntions for this example each are a oncollision functions with different collision gameobjects
so is it more optimal overall to use along script or short ones
its a 2d game would I ever use vector 3.zero?
and rite now the short scripts include oncollision function for enemy and player etc so for example player dies when he hits certain things and or walls so I made 3 separate short scripts for each on collision s since each collider has a different tag
and please tell me from your experience --- overall is it more optimal to use many short scripts or a long one?
It is usually bad if you can't reuse a script since if you need a variation of your script you need to copy paste some parts, which introduce more maintenance. It is however also bad if you over complicate things only to future proof it especially in games since you normally have a very short time to complete things. Refactoring the code when needed is usually what I do.
The Vector3.zero example was just an example. The same is true for Vector2.zero and other static variables and methods.
Since Unity is mostly component based it makes sense to continue that ins$$anonymous$$d of setting up a lot of dependencies. This often means that you get smaller scripts, however they should not be so tiny that they could be anything. The performance would perhaps suffer a little bit but it is often outweighed by the productivity time. Like most game developers experience it the majority of the code for a game is not performance critical. Perhaps 2% to 10% of the game should be heavily optimised while the other 90%+ could perhaps yield a few percentage increase in performance if optimized.
Answer by Chikari · Feb 05, 2017 at 10:55 AM
This depends on what your definition of "optimal" is.
As @Pengocat mentioned, if you put more functionality into one script, it becomes less useable. Say you have an object A_1 that checks for collision with B and C and an object A_2 which should only check for collision with object B. You could drag both scripts CollisionB and CollisionC onto A_1, and only CollisionB onto A_2. This would not not be possible if both collision checks were in one script (..without modifications..).
If you are referring to performance: If you split functionality into two scripts, you're going to have two function calls. Script1.doSomething() and Script2.doSomething(). A function call does require "some time", so having two is slower. But that overhead is extremely small. Also, the compiler might "inline" both functions. In this case, the performance difference is basically zero.
However, if you split up functionality into too many files, maintenance might get messy. Say you want to change the way doSomething() does it's thing: If it's in one file, you can change it alltogether. If it's in multiple files, you might miss something out.
So concluding... it really depends on your script's functionalty, how often it's going to be used and how many scripts you have. Performance can be neglected imo.