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?.

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