Optimization and Good habits programming whit C#

I have searched about good practices, but only found things like “comment your code” or “careful when naming your variables”.

I would like to know things like:

  • Don’t use getComponenet( ) in functions that will be called several times.
  • it is preferable to use Vector instead List because it is lighter.

And I have some doubts:

  1. Use pool of Gameobjects instead of instantiating and destroying objects, is a good practice?
  2. I must separate the responsibilities of the script?
    For example a player script: movement and shooting system in the same scrpt or separated?

A quick google search will turn up hundreds of documents and articles on this topic. Most of it is general coding best practices. Far too broad a topic to be answered here, you need to be more specific.

Here’s a good article to get you started: http://www.gamasutra.com/blogs/HermanTulleken/20160812/279100/50_Tips_and_Best_Practices_for_Unity_2016_Edition.php

In answer to your specific points: instantiating and destroying objects is much slower/more processor intensive than simply turning them off and on again. So yes, pools are good.

General coding best practice tells you that a single code file should only do one thing. So yes, have separate scripts for separate tasks. If you, as in your example, split your movement and shooting code into separate scripts it makes things much easier to later build a turret, that shoots but does not move, or a passive character, who moves but does not shoot.