hi since my basic scripts of my project is nearly done. i was curious: does that fact that i putted all my coding in the fewest amount of script as possible(for example the entire AI codes in one script and the entire cods related to the player in one script) going to make troubles for me? cuz every people i see all of them are like having 100 scripts in each only 1 line (i mean based on what i saw on youtube tutorials and etc. )
so if you donāt count the fact that how much hard or easy is going to be for me having the entire codes in fewer scripts.technically and based on performance values, which is better? separating my script files into fewer one, or hold the them as it is now,(all in one)
In order to get maintainable code, splitting the scripts into meaningful pieces is absolutely necessary. If you end up animating the character in your AI script, you made something wrong. If you have tons of AI functionality for all kinds of different characters in the same script, you should consider to find a better solution.
From a performance point of view, there will not be a noticeable difference if you make it good enough. E.g. you may initialize the connections between those scripts in Awake (), like GetComponent and so on. You may also use some kind of manager class that is responsible for all instances of a certain type. There are tons of possibilities.
It depends on the project needs and also the way that the creator is organizing. I guess the only thing that make a real difference in performance is how many Update function you have. The less you have, the better it is. But you canāt compress that number too much. For exemple, in a standard game, all your ennemies should have a script with an update function. Donāt make a general script that will have to communicate with each of the ennemies in order to make them move or attack or whatever. Unity use Object coding. You need to think that way when coding. If all your ennemies have a single script attached that make them move, attack and so on itās fine(but most people would do a script for movement, a script for attack and so on), but every ennemies should have a script attached. I hope thatās what you mean by āThe entire AI is in one scriptā and not that you have a Single script that make moves all your AI agents.
One of the main things we do as programmers is break large problems down into smaller ones. Large problems are hard or impossible to handle all at once.
Classes help us by allowing us to think about only a few small problems at once. We generally put each class into itās own file.
Having a giant file with an entire gameās logic in it at once is the opposite of breaking a complex problem into smaller, easier-to-solve, problems.
Better to have 100 Update functions on a character that works correctly than 1 Update function on a character the breaks the game.
Code for correctness and maintainability first. A 5-line script is easier to maintain and easier to test for correctness than a 500-line script.
Then profile to identify bottlenecks. Itās pointless to optimize before profiling. After profiling, donāt optimize ā re-evaluate. Itās better to step back and change to a smarter algorithm than waste time trying to optimize a less-efficient algorithm.
Or, in the context of your question, re-evaluate might mean identifying what scripts are giving you problems. Maybe the AI script is getting too big and complicated to modify without introducing bugs. In this case, re-evaluate how you could divide it into separate scripts like the folks above wrote, such as one for movement, one for targeting, one for tactics, etc.
I try to minimize MonoBehaviours, dependence on the Update() cycle, and time spent configuring things in the editor.
That said, I still try to organize my code into single responsibility types and functions. Itās just that decomposing into MonoBehaviours runs counter to robust event ordering, interactions, and the use of fundamental design principles like constructors, interfaces, and generics.