- Home /
The question is answered, right answer was accepted
Number of scripts per Object. Optimization question.
Is it better to have one script with everything in it like player health, player ID(which includes things like what faction it belongs to etc) and say things like the movement controller or would it make a difference to cpu if I separate them? Would it be better or worse for CPU or won't it make a difference at all?
I think it would be better to separate no? Then codes have less chance of errors too right?
You guys are the experts so I thought it would be good to ask you.
So will it make difference to cpu if have such things in separate scripts on a gameobject?.
This may be a bit off topic to your question but personally I recommend specific scripts to have specific jobs. Not necessarily what you're referring to with like player movement/network/etc but "managers". So for example I have a PlayerPref$$anonymous$$anager. Any time I'm setting/retreaving anything to do w\ PlayerPrefs I make a method inside my PlayerPrefs$$anonymous$$anager class and then from in that class I actually access the PlayerPrefs. This way all my scripts are calling 1 function that is 100% the same. So if I ever decide to change the name of my pref, or maybe the default value returned if its null I don't have to spend 1 hour searching through all my classes to see who calls the playerprefs. I just change it within my PlayerPref$$anonymous$$anager.
This is when I tend to make things a new script. For the most part I try to make every script have 1 sole job. HighScores$$anonymous$$anager, $$anonymous$$apUnlock$$anonymous$$anager, Player$$anonymous$$anager, SaveGame$$anonymous$$anager, PlayerPrefs$$anonymous$$anager, Audio$$anonymous$$anager, etc.
I can't speak for performance but I can speak for ease of use for me, the developer, and it helps more then you'd think.
@tris9556 +1 cause I do the same things, however only for "manager" scripts (which in most cases are static classes so it doesn't actually matter). But for something like a movement script, you wouldn't split it up into: Collision, Input, $$anonymous$$ovement, Networking, etc... You'd usually have it all in one script
Answer by Benproductions1 · Sep 15, 2013 at 11:03 PM
Hello,
Generally I like to think of your problem like this:
Imagine your job is to carry a certain amount of apples from point A to point B. Your problem is: How to carry them. You have an infinite supply of various sizes of bags.
Would it be easier to wrap each apple in a seperate bag and try to carry all of them, or would it be easier to put them all in one bag?
The simple answer is: It depends.
Generally I preffer to have one single script per functionality. One script for movement (the player), one script for networking (in general), one script for the network server, etc...
Depending on complexity that usually results in one script per gameplay object and a couple scripts in your "game manager".
Doing things all in one script (as long as it's not too long) has the following advantages:
1. It's easier to manager. You get a better overview of all your functionality and you don't have to go though 20 scripts trying to find your logic error.
2. It's less prone to errors. Depending on what you are doing, having everything in one script will be less prone to errors, because of the lack of references needed for it to work and the lack of dependancy on other scripts.
3. It compiles faster. If you're using C# it doesn't matter, but with Unityscript the compile time is hurt quite badly when the number of scripts increases.
4. It is (however unnoticably) faster to have everything in one script. If you're using more than one script, you will need to reference the other scripts for communication between them. Rather than doing someFunction()
you will do someInstance.someFunction()
, meaning one extra look up, resulting in slower code.
5. It takes up more memory. Back to ther references, you are also storing one extra pointer in memory for every reference, when you really don't need to (yes, this really doesn't make any difference)
6. Less scripts, means less objects. Less objects means faster game. There is quite a significant difference between having 20 objects with scripts and 200 objects with scripts. It does slow down considerably, because of the way Unity works.
Honestly, it really depends on your situation. If you have a bunsh of math functions you need from multiple scripts, put them in a seperate file in a static class. If you have a rather small script, there is no need to split it up. If you have a script that really does 2 seperate things and it's quite long, it would be appropreate to split it up. If you have any functionality that is needed from more than one script, you should always split it up.
Now that i've answered your question, I must say that it is a very subjective topic and not really appropreate for Unity Answers and should get posted in the forums.
Hope this helps,
Benproductions1
Ah ok thanks, that answers my question.
Also I think it was pretty specific. You answered it just fine.
Anyway thanks.
@Dead$$anonymous$$enny, I never said it wasn't specific. I said it was subjective. Everyone will give you a different answer and everyone will do it a bit differently. That's why it doesn't belong on Unity Answers, because there is no "right answer"