C# to Javascript conversion help

I’ve been trying to convert a script from C# to Javascript (with some success), but now I’m stuck with some errors in my console which I cannot figure out… The script I’m trying to convert is a script from catlikecoding.com;

using UnityEngine;
using System.Collections.Generic;

public class PlatformManager : MonoBehaviour {

	public Transform prefab;
	public int numberOfObjects;
	public float recycleOffset;
	public Vector3 startPosition;
	public Vector3 minSize, maxSize, minGap, maxGap;
	public float minY, maxY;

	private Vector3 nextPosition;
	private Queue<Transform> objectQueue;

	void Start () {
		objectQueue = new Queue<Transform>(numberOfObjects);
		for(int i = 0; i < numberOfObjects; i++){
			objectQueue.Enqueue((Transform)Instantiate(prefab));
		}
		nextPosition = startPosition;
		for(int i = 0; i < numberOfObjects; i++){
			Recycle();
		}
	}

	void Update () {
		if(objectQueue.Peek().localPosition.x + recycleOffset < Runner.distanceTraveled){
			Recycle();
		}
	}

	private void Recycle () {
		Vector3 scale = new Vector3(
			Random.Range(minSize.x, maxSize.x),
			Random.Range(minSize.y, maxSize.y),
			Random.Range(minSize.z, maxSize.z));

		Vector3 position = nextPosition;
		position.x += scale.x * 0.5f;
		position.y += scale.y * 0.5f;

		Transform o = objectQueue.Dequeue();
		o.localScale = scale;
		o.localPosition = position;
		objectQueue.Enqueue(o);

		nextPosition += new Vector3(
			Random.Range(minGap.x, maxGap.x) + scale.x,
			Random.Range(minGap.y, maxGap.y),
			Random.Range(minGap.z, maxGap.z));

		if(nextPosition.y < minY){
			nextPosition.y = minY + maxGap.y;
		}
		else if(nextPosition.y > maxY){
			nextPosition.y = maxY - maxGap.y;
		}
	}
}

This is exactly what I’m looking for, and would be an excellent starting point for me, but as I’ve stated I’m having some problems converting it. This is what I have so far;

#pragma strict

public var prefab = Transform;
public var numberOfSegments : int; 
public var recycleOffset : float; 
public var startPosition : Vector3;
 
public var minSize : Vector3;
public var maxSize : Vector3;
public var minGap : Vector3;
public var maxGap : Vector3;

public var minY : float; 
public var maxY : float;

private var nextPosition : Vector3;
private var segmentQueue : Queue = new Queue();


function Start () {

	segmentQueue = Queue(Transform(numberOfSegments));
		for (var i : int = 0; i < numberOfSegments; i++) {
	segmentQueue.Enqueue(Instantiate(Transform, prefab));
		}
		
		nextPosition = startPosition;
		for (var j : int = 0; j < numberOfSegments; j++) {
			Recycle();
		}

}

function Update () {

	if (segmentQueue.Peek().Position.x + recycleOffset < Runner.distanceTraveled) {
		Recycle();
	}

}

function Recycle () { 
	
	var scale = Vector3; {
		Random.Range (minSize.x, maxSize.x);
		Random.Range (minSize.y, maxSize.y);
	}
	
	var position : Vector3 = nextPosition; 
		position.x += scale.x * 0.5f;
		position.y += scale.y * 0.5f;
		
	var o : Transform = segmentQueue.Dequeue();
		o.localScale = scale;
		o.localPosition = position;
		segmentQueue.Enqueue(o);
		
	nextPosition += Vector3; {
		Random.Range(minGap.x, maxGap.x) + scale.x;
		Random.Range(minGap.y, maxGap.y);
		Random.Range(minGap.z, maxGap.z);
	}
	
	if (nextPosition.y < minY) {
		nextPosition.y = minY + maxGap.y;
	}
	
	else if (nextPosition.y > maxY) {
		nextPosition.y = maxY - maxGap.y;
	}
		
	
}

I’m down to eight errors in the console;

  • (22,39): BCE0024: The type ‘UnityEngine.Transform’ does not have a visible constructor that matches the argument list ‘(int)’.

  • (24,41): BCE0023: No appropriate version of ‘UnityEngine.Object.Instantiate’ for the argument list ‘(System.Type, System.Type)’ was found.

  • (36,33): BCE0019: ‘Position’ is not a member of ‘Object’.

  • (50,37): BCE0019: ‘x’ is not a member of ‘System.Type’.

  • (51,37): BCE0019: ‘y’ is not a member of ‘System.Type’.

  • (55,32): BCE0022: Cannot convert ‘System.Type’ to ‘UnityEngine.Vector3’.

  • (59,22): BCE0051: Operator ‘+’ cannot be used with a left hand side of type ‘UnityEngine.Vector3’ and a right hand side of type ‘System.Type’.

  • (60,58): BCE0019: ‘x’ is not a member of ‘System.Type’.

I’m still kinda new to the whole JavaScript/UnityScript scene, so most of these don’t make any sense to me… Any help is much appreciated!

Have a look at the numbers at the beginning of each line. They tell you where the error is.

For example:

(22,39): BCE0024: The type ‘UnityEngine.Transform’ does not have a visible constructor that matches the argument list ‘(int)’.

Refers to this line:

segmentQueue = Queue(Transform(numberOfSegments));

That means that you can’t create an object of type Transform, using a constructor that receives a single integer as argument, in this case “numberOfSegments”.

But the original line has this code:

objectQueue = new Queue<Transform>(numberOfObjects);

Which is using a generic method, passing the Transform type. Translated to UnityScript you should write something liket this:

var objectQueue = new System.Collections.Generic.Queue.<Transform>(numberOfObjects);

which is the correct way of calling generic methods in UnityScript (‘.’).

You can remove some code by adding this line just before the #pragma stric statement

#pragma stric
import System.Collections.Generic;

And the the line would become

var objectQueue = new Queue.<Transform>(numberOfObjects);

And that’s it. You can continue with each error, one at the time.

Although I strongly suggest that you stick to C# if you can as you will find there is a lot more documentation for .Net in C# available online, as well as online tutorials for Unity. The difference is not that big, it won’t take you more than a week to learn the basics of C#.

Things like

var scale = Vector3; {

are wrong; just use the C# code. It’s the same as Unityscript. (Though you can optionally leave out “new”.)