Overview: Writing Scripts in C# Manual     Reference     Scripting  
Scripting
Overview: Writing Scripts in C#

Apart from syntax, there are some differences when writing scripts in C# or Boo. Most notable are:

1. Inherit from MonoBehaviour

All behaviour scripts must inherit from MonoBehaviour (directly or indirectly). This happens automatically in Javascript, but must be explicitly explicitly inside C# or Boo scripts. If you create your script inside Unity through the Asset -> Create -> C Sharp/Boo Script menu, the created template will already contain the necessary definition.

public class NewBehaviourScript : MonoBehaviour {...} // C#

class NewBehaviourScript (MonoBehaviour): ... # Boo

2. Use the Awake or Start function to do initialisation.

What you would put outside any functions in Javascript, you put inside Awake or Start function in C# or Boo.

The difference between Awake and Start is that Awake is run when a scene is loaded and Start is called just before the first call to an Update or a FixedUpdate function. All Awake functions are called before any Start functions are called.

3. The class name must match the file name.

In Javascript, the class name is implicitly set to the file name of the script (minus the file extension). This must be done manually in C# and Boo.

4. Coroutines have a different syntax in C#.

Coroutines have to have a return type of IEnumerator and you yield using yield return ... ; instead of just yield ... ;.

using System.Collections;
using UnityEngine;
public class NewBehaviourScript : MonoBehaviour {
// C# coroutine
IEnumerator SomeCoroutine () {
// Wait for one frame
yield return 0;

// Wait for two seconds
yield return new WaitForSeconds (2);
}
}

5. Don't use namespaces.

Unity doesn't support placing your scripts inside of a namespace at the moment. This requirement will be removed in a future version.

6. Only member variables are serialized and are shown in the Inspector.

Private and protected member variables are shown only in Debug Mode. Properties are not serialized or shown in the inspector.

7. Avoid using the constructor.

Never initialize any values in the constructor. Instead use Awake or Start for this purpose. Unity automatically invokes the constructor even when in edit mode. This usually happens directly after compilation of a script, because the constructor needs to be invoked in order to retrieve default values of a script. Not only will the constructor be called at unforeseen times, it might also be called for prefabs or inactive game objects.

Using the constructor when the class inherits from MonoBehaviour, will make the constructor to be called at unwanted times and in many cases might cause Unity to crash.
Only use constructors if you are inheriting from ScriptableObject.

In the case of eg. a singleton pattern using the constructor this can have severe consequences and lead to seemingly random null reference exceptions.

So if you want to implement eg. a singleton pattern do not use the the constructor, instead use Awake. Actually there is no reason why you should ever have any code in a constructor for a class that inherits from MonoBehaviour.