Skip to main content

Format code examples

Use clear and consistent formatting for code examples to make the code easy to read and understand.

Follow the guidance on this page alongside the Unity coding reference and Microsoft guidelines.


Use explicit types to declare the data type of a variable. Explicit types provide greater clarity and allow the doc tools to automatically add links to the Unity types used in the code sample.

var hinge = otherGameObject.GetComponent<HingeJoint>();HingeJoint hinge = otherGameObject.GetComponent<HingeJoint>();


Place opening and closing braces on their own line, at the same level of indentation as their parent, like this:

void Update(int myVar)
if (myVar == 10)

For more advice on formatting code, refer to the Unity coding standards.


Place spaces on either side of parentheses, except in function calls and function definitions. If you have more than one parenthesis, don’t put spaces between them. For example:

  • if (myVar == 10)
  • MyFunc(myParam);
  • MyFunc();
  • MyFunc(a * (b + 10));


Place spaces on either side of mathematical symbols (except negation of a value). For example:

  • a = 10;
  • b = 10 - a;
  • c *= -10;
  • c /= -b;


Comments in scripting examples should be short and concise, and adhere to the same style guidelines as regular text.

Place comments in a line above the corresponding piece of code. For example:

// Unity calls code in the update function every frame
void Update()
// Print something to the console

If you write one comment and multiple lines of code, place one empty line above the comment to visually group the comment with all the code lines. For example:

void Update()
// Move the player a bit to the right
pos.x += Time.deltaTime;

// If the player has gone far enough, load the next level
if (pos.x >= 1000)
score += 100;