C# Code into JS - I have problem with some part

I wanna convert a C# script into UNITY Javascript. A lot of code is clear but there are a few lines where I stumble:

public enum CollisionStyle
{
	Left,
	Right
}
public LayerMask collisionLayers = new LayerMask();
public CollisionStyle c_Style = CollisionStyle.Left;

public Transform target;

Here I thought its

enum CollisionStyle {Left, Right};
var collisionLayers = new LayerMask() : LayerMask;
	
var c_Style : CollisionStyle = CollisionStyle.Left;

var target : Transform;

I get a “Parsing Error” at Line “var target : Transform;”

Can anybody help, please?

enum CollisionStyle {Left, Right}
var collisionLayers : LayerMask = new LayerMask();
       
var c_Style : CollisionStyle = CollisionStyle.Left;
     
var target : Transform;

First thanks. The other Errors appears.
Now I have the following problems:

public List<Renderer> objectsToFade;

	private List<Material> _faded_mats = new List<Material>();
	private List<Material> _current_faded_mats = new List<Material>();

	public bool Controllable
	{
		get { return _controllable; }
		set { _controllable = value; }
	}

      foreach(Material m in r.materials)

set it to:

	var objectsToFade : List[Renderer];

	private var _faded_mats : List[Material] = new List[Material]();
	private var _current_faded_mats : List[Material] = new List[Material]();


	// Dont know what this is
        public bool Controllable
	{
		get { return _controllable; }
		set { _controllable = value; }
	}

     case of (Material m in r.materials)

Well, this

public bool Controllable {
	get { return _controllable; }
	set { _controllable = value; }
}

in C# is a property. It is like when you define a private variable and then you use 2 methods to access that variable:

private bool _controllable;

public bool GetControllable() {
	return _controllable;
}

public bool SetControllable(bool parameter) {
	_controllable = parameter;
}

With properties you can access the value of the object like

Controllable = false;

or

if(Controllable) {
   // do something...
}

while if you use the methods approach you have to

SetControllable(false);

or

if(GetControllable()) {
   // do something...
}

If you understand what properties are, now you are able to translate in JavaScript :wink:

Oh my goodness…I see I have so much to learn but its really difficult.

What about

private List<Material> _faded_mats = new List<Material>();

I thought its an array, but

private var _faded_mats : List[Material] = new List[Material];

doesn’t function.

Many thanks…

Ok,

private List<Material> _faded_mats = new List<Material>();

is a general linked list filled with objects of type Material so you cannot have another type of objects in that list.
I don’t know if in JavaScript you have a generic linked list collection but I think you can use:

private var _faded_mats : Material[];

but the think is that if you will need to push and pull from the array, you have to provide your own methods.

At this point my JavaScript knowledge is in a dead end because I don’t know how to use arrays.

Maybe this link
http://www.w3schools.com/js/js_obj_array.asp
can help you !!! :wink:

List<> is from Generics.
Take a look at this topic. This may help you.

Doubtful - that’s web JS which doesn’t apply to Unity at all.

I’m curious why OP is attempting to translate the code in the first place instead of just using it…

Hi,
I just translate the C# Code into Java, cause I began to write in Java a Testgame, then I found a C# script which I can take and its helpful, but I just manipulate a variable in the C# Script with a Java-Script, but I don’t know how.
So for me it is helpfull to translate the C# Script into Java, and make one hole Java Script instead of one Java + 1 C# Script.

On this Page: http://wiki.unity3d.com/index.php?title=Which_Kind_Of_Array_Or_Collection_Should_I_Use%3F
I found something about “List”, but that doesen’t work

var objectsToRotate : List.<Transform> = new List.<Transform>();

On Wiki the decleration is just writen so, but UNITY 3.5.5 says: BCE0018: The name ‘List’ does not denote a valid type (‘not found’). Did you mean ‘UnityEngine.Light’?

Whats wrong?

var objectsToRotate = new List.<Transform>();

brings the same ERROR !

Anybody knows how to write correct?

List is in the System.Collections.Generic namespace. You need to import that first.

Many thanks I found it:

import System.Collections.Generic;

Hello:
I only want to say that now I am finished transforming c# to JAVA in UNITY and I am glad that all works fne.

Thanks all for the help. Best wishes from the Black-Forest Germany to all Users. You are the best…best forum here…