Translating Script From Js to C# - Wired sentence

Translating Script From Js to C# - Wired sentence
I have Read and saved pages that show the difference between JavaScript and C#.

In addition I have all JS Pages open in front of me.

But I can’t find example to what this sentence is in JS:

private var thumbComponents : Component;

I don’t understand, this is not how you create an ARRAY in JS as far as I saw on the net.

Its a controller Script We have Bought and I am translating it,

On the script :

private var thumbComponents : Component;

Under Start function:

guiThumbArea = thumbComponents[0];

guiThumbFinger = thumbComponents[1];

(those 2 other variables are also declared but I just not show them to keep post clean)

The obvious way to translate it will be

private var thumbComponents : Component; - > private Component thumbComponents;

But I don’t think it will be true because I need to Use index numbers on that variable

later on under Start Function as shown.

Please help me both to understand what it is, and what will be the correct way to translate it.

Thank you.

Firstly, JavaScript in Unity is not normal JavaScript. It is UnityScript, which is an alteration of JavaScript, and so it is not syntactically identical, in many ways.

However, the C# equivalent of

private var thumbComponents : Component[];


private Component[] thumbComponents;

The biggest things to worry about with UnityScript, converting to C#:

  1. In C#, everything must be in a class or struct, apart from using statements and namespace declarations.
  2. UnityScript does not necessarily make use of the “new” keyword. All instantiations (i.e. directly calling the constructor of a class or struct) in C# MUST use the new keyword.
  3. In UnityScript, the ‘:’ operator is used to define the type of something. For example: private var thumbComponents : Component[]; is a statement that says “I have a variable that is of type Component array.”
  4. UnityScript uses functions, which are always started with the keyword “function,” and (if a type is specified), followed with a colon (‘:’) which is then followed by the return type. C# specifies the return type in place of the “function” keyword, and does not use colons in this way.

Those are the big ones. There are a lot of other, lesser differences, but they are pretty specific to the situation.

Also: Please put code in code blocks when putting up a question, answer, or comment.


The process of instantiating and populating an array that is a member field (not static), in a Unity MonoBehaviour object, in C# can be done with the following pattern:

private Component[] components0;
private Component[] components1;

void Start()
    // One of the more common methods of initializing and filling an array: use a loop.
    components0 = new Component[someSize];// Where someSize is an integral number
    for(int i = 0; i < someSize; i++)
        components0 *= GetComponent<SomeComponentType>();*

// Arrays can also be instantiated and populated at the same time.
// Put a number between the two []s to specify the length of the array you
// will be defining, to ensure that it is only a specific length.
// If you leave them blank, it will dynamically size the array, upon instantiation,
// to the appropriate size.
components1 = new Component[]
// Etc…
These methods can be used with virtually any type, with C#. However, many of these patterns are not unique to C#, and I would suggest learning some basics of C-based language programming before going much further.
Also, I’ve left out some pretty important parts, here, for the sake of simplicity (and because I don’t believe in doing more than is necessary to explain the logic, when it comes to answering these types of questions).