Apart from syntax, there are some differences when writing scripts in C# or Boo. Most notable are:
除了语法外,使用C#或Boo会有一些差别,最明显的是:
1. Inherit from MonoBehaviour
继承之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.
使用Awake或Start方法进行初始化。
What you would put outside any functions in Javascript, you put inside Awake or Start function in C# or Boo.
,你(需要)在C#或Boo在使用Awake或Start方法。
The difference between Awake and Start is that Awake is ***n 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.
Never initialize any values in the cons***ctor. Instead use Awake or Start for this purpose. Unity automatically invokes the cons***ctor even when in edit mode. This usually happens directly after compilation of a script, because the cons***ctor needs to be invoked in order to retrieve default values of a script. Not only will the cons***ctor be called at unforeseen times, it might also be called for prefabs or inactive game objects.
So if you want to implement eg. a singleton pattern do not use the the cons***ctor, instead use Awake. Actually there is no reason why you should ever have any code in a cons***ctor for a class that inherits from MonoBehaviour.