Comparison between the two languages in unity3d JavaScript vs C # Section 1

Source: Internet
Author: User

 

When unity3d is used for game development or interactive project creation, there are three editing languages available, but the most used is JavaScript and C, I believe that many friends, especially new users, will be very entangled in which one of the two is better. The following is an effective comparison between the two languages in unity3d by foreigners, this is the first chapter. It mainly compares the similarities and differences between the two on common basic points such as variables, classes, and functions!
This is the first post of a series that will discuss some differencesJavascriptAndC #When programming for unity3d game engine. A project with all the scripts used in these posts will be available for download in the last post
The series. So let's start from the start and see some of the main differences between the two ages.

The first and most easily distinguishable is the notation used to declare variables and methods. When programming inJavascript, It isn' t necessary to declare the variable or method type whether inC #,It's a must. That is because Javascript
IsWeakly typedLanguage, meaning that the interpreter chooses the best type to use at compilation-time.C #Is a totally different because it isStronugly typed, So, the type has to be declared before a variable
Or method. Let's see some examples to better grasp this concept. The following script isJavascriptExample:
Private var cubetransform;

And inC #, The same code wocould be:
Private transform cubetransform;

This is also valid for methods. Even ifC #Method returns nothing,VoidType has to be declared as the return type, which can be omitted in JavaScript.

Class inheritance also works differently. For bothJavascriptAndC #, Methods are implicitly final and can't be overridden, unlessVirtualKeyword is added to the method declaration (for the two programming languages ).
The difference is thatC #Only overrides methods that containOverrideKeyword. On the other hand,JavascriptTries to override the class methods as soon as it inherits them, no override keyword needed. So, as an Example
Of class inheritance for Javascript, we will have:

Class weapon extends item

{

// Class members and declarations

}

 

The same code written in C # will be:

Public class weapon: item

{

// Class members and declarations

}

 

That's the main differences between the two versions and it will practically define all others, such as yielding code execution, accessing gameobjects and components, raycasting, etc. there are other differences such as the keyword used to import Libraries
("Import"JavascriptAnd"Using"For C #) but these declaration and keyword differences are easily figured out.

So, if the reader understood these differences, all the other ones that will be explained on the next posts will make more sense.

One final last thing: Even if you don't have to declare the type of the variables and methods in Javascript it is recommended that you do. this is due the fact that the compiler tries to figure out the type of variable to use, if it hasn't been declared,
Which may lead to performance issues. e.g .:

// Do this:

Private var score: int;

// Instead of this:

Private var score;

 

This wouldn't be a issue if we were talking about code that runs every minute, but let's not forget that games today runs practically at 60 FPS, and unity3d games are no exception. this way, add variable types whenever you can when programmingJavascriptFor
Unity3d.

In the next post, differencesC #AndJavascriptWill be shown when programming a script that access other gameobjects and components. You can read it here:

Section 2: Object objects and component attributes gameobjects and components.

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.