Overview: writing scripts in C # use C # To write scripts

Source: Internet
Author: User

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

In addition to syntax rules, there are some differences between using C # Or boo to write scripts. When you need to pay special attention:

1. inherit from monobehaviour
Inherited 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.

All behavior scripts must be inherited (directly or indirectly) from monobehaviour ). this is automatically completed in Javascript, but it must be displayed in C # Or boo. if you use asset-> Create-> C sharp/Boo script to create a script, the system Template already contains the necessary definitions.

Public class newbehaviourscript: monobehaviour {...} // C #
Class newbehaviourscript (monobehaviour):... # boo

2. Use the awake or start function to do initialisation.
Use the awake or start function for initialization.

What you wocould put outside any functions in Javascript, you put inside awake or start function in C # Or boo.

JavaScript code that is placed outside the function must be placed in the 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.

The difference between awake and start is that awake runs when loading a scenario. Start is called before the first call of the update or fixedupdate function, and awake runs before all start functions.

3. The class name must match the file name.
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.

The class name in Javascript is implicitly set as the script file name (excluding the file extension). You must manually write the class name in C # And boo.

4. coroutines have a different syntax in C #.
C # different syntaxes and rules apply to the cooccurrence program.

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

Coroutines must be returned by ienumerator, and yield should be replaced by yield return.

Using system. collections; using unityengine; public class newbehaviourscript: monobehaviour {// C # coroutine // C # collaborative program ienumerator somecoroutine () {// wait for one frame yield return 0; // wait for two seconds yield return New waitforseconds (2 );}}

5. Don't use namespaces.
Do not use namespace

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

Currently, Unity does not support namespaces. It may be available in future versions. (You can customize classes, but you cannot use namespaces)

6. Only member variables are serialized and are shown in the inspector.
Only serialized member variables can be displayed on the view panel.

Private and protected member variables are shown only in EXPERT mode. properties are not serialized or shown in the inspector.

Private and protection variables can only be displayed in EXPERT mode. properties are not serialized or displayed in the view panel.

7. Avoid using the constructor.
Avoid using Constructors

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.

Do not initialize any variables in the constructor. use the awake or start function. even in the editing mode, unity automatically calls the constructor. this is usually after a script is compiled, because you need to call the script constructor to retrieve the default value of the script. we cannot predict when to call the constructor. It may be called by a preset or inactive game object.

In the case of EG. A Singleton pattern using the constructor this can have severe consequences and lead to seemingly random null reference exceptions.

Using constructor in a single mode may cause serious consequences and cause random null parameter exceptions.

So if you want to implement eg. A Singleton pattern do not use the constructor, instead use awake. actually there is no reason why you shoshould ever have any code in a constructor for a class that inherits from monobehaviour.

Therefore, if you want to implement a single mode, do not use constructor. In fact, you do not need to write any code in the constructor that inherits the class monobehaviour.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.