Unity 3D – Best practices for script development

You will find on this page all good practices you must know to keep your scripts easely maintenable and well architectured. This is very important if you plan to work with other programmers

All shorcuts given are available for Microsoft Visual Studio


Keep your code well indented

Shortcut : CTRL + K, CTRL + D

A basic of developpement is to keep all your code indented correctly in order to read it more easely.

Keep your code organized with « Regions »

Shortcut : CTRL + K, CTRL + S

I Suggest you to keep your code organized with regions in order to navigate more quickly to where you need to go

You can also roll or unroll the entire pretty simply

Enroll / Roll : CTRL + M, CTRL + M

Always comment your properties or your functions

Just type "///" above the property or the function and Visual Studio will generate automatically the necessary lines 

It’s very important to know for what the property or the function is useful for mostly if you don’t have written it.

Furthermore, the visual studio auto completion will show you all this stuff when you will write your code :

Name all your parameters with a starting “p” letter

When you write a function, i highly suggest you to start all your parameters name with a “p” to clearly and quickly identify what is a class property and what is a parameter.

Always Create an Init() function

A good practice is too use an Init() function wich can take parameters to initialize your component. Beginners put all this stuff directly in the Start() function. 

Having alway an Init() force you and your team to initialize always all your components by the same way. You can then call this function wherever you want.

If your script is auto initialized, you then can call this function on the Start() unity callback :

If you GameObject is instantiated by a spawner for exemple, you just need to call the function after having instantiate the GameObject.

By doing that, you will have a total control of when the Init() function is called !

Names your properties and function clearly

I have seen a lot of projects where programmers give name to their properties who has no sense with the usage of them

It’s really important to give a name wich identify perfectly the usage of your property of function. Here are some examples :

    #region Properties

    /// <summary>
    /// List of connected clients
    /// </summary>
    public List<int> clientsId;

    /// <summary>
    /// Reference to the DarkRift server
    /// </summary>
    public XmlUnityServer serverReference;

    /// <summary>
    /// List of objects handled by the server
    /// </summary>
    public List<NetworkObject> networkObjects;

    /// <summary>
    /// Frame counter on the server
    /// </summary>
    public int currentServerTick = 0;


Keep your functions and properties names correctly written

It’s important to use name convention when you are coding. Here is a complete article about the subject i higly recommands you to read. If you’re lazy, here are the great guidelines :

  1. Don’t use underscore to name your properties / functions
  2. Class and methods start withs a Capital Letter
  3. Properties and variables always starts with a tiny letter
  4. Don’t use abbreviation
Note : This article is continuously updated

Posted in Best Practice.