Help me Convert JS to C#

Hello, i’m beginner to unity, and i only use Javascript, today i was forced to convert my js script to C#, always fails when because i don’t know much about arrays, this is the JS

var waypointContainer : GameObject;
private var waypoints : Array;
private var currentWaypoint : int = 0;

function Start () {
    var potentialWaypoints : Array = waypointContainer.GetComponentsInChildren( Transform );

    waypoints = new Array();

    for ( var potentialWaypoint : Transform in potentialWaypoints ) {

    if ( potentialWaypoint != waypointContainer.transform ) {
    waypoints[ waypoints.length ] = potentialWaypoint;
    }

    }
}

function Update () {

}

function NavigateToWaypoint () {
var RelativeWaypointPosition : Vector3 = transform.InverseTransformPoint( Vector3( waypoints[currentWaypoint].position.x, transform.position.y, waypoints[currentWaypoint].position.z ) );
//AxisCarController.steerAI = RelativeWaypointPosition.x / RelativeWaypointPosition.magnitude;

if ( RelativeWaypointPosition.magnitude < 15 ) {
currentWaypoint ++;

if ( currentWaypoint >= waypoints.length ) {
currentWaypoint = 0;
}


}
}

And this is the C# script

using UnityEngine;
public class GetWayPoint : MonoBehaviour
{
    // Fields
    private int currentWaypoint;
    public GameObject waypointContainer;
    private Array waypoints;

    // Methods
    public override void Main()
    {
    }

    public override void NavigateToWaypoint()
    {
        if (this.get_transform().InverseTransformPoint(new Vector3(RuntimeServices.UnboxSingle(UnityRuntimeServices.GetProperty(UnityRuntimeServices.GetProperty(this.waypoints.get_Item(this.currentWaypoint), "position"), "x")), this.get_transform().get_position().y, RuntimeServices.UnboxSingle(UnityRuntimeServices.GetProperty(UnityRuntimeServices.GetProperty(this.waypoints.get_Item(this.currentWaypoint), "position"), "z")))).get_magnitude() < 15)
        {
            this.currentWaypoint++;
            if (this.currentWaypoint >= this.waypoints.get_length())
            {
                this.currentWaypoint = 0;
            }
        }
    }

    public override void Start()
    {
        Array componentsInChildren = this.waypointContainer.GetComponentsInChildren(typeof(Transform));
        this.waypoints = new Array();
        IEnumerator enumerator = UnityRuntimeServices.GetEnumerator(componentsInChildren);
        while (enumerator.MoveNext())
        {
            object obj1 = enumerator.get_Current();
            if (!(obj1 is Transform))
            {
            }
            Transform transform = RuntimeServices.Coerce(obj1, typeof(Transform));
            if (transform != this.waypointContainer.get_transform())
            {
                this.waypoints.set_Item(this.waypoints.get_length(), transform);
                UnityRuntimeServices.Update(enumerator, transform);
            }
        }
    }

    public override void Update()
    {
    }
}

it’s giving me the error that the namespace Array couldn’t be found.

Please help me :frowning:

Is that the only error you’re getting? - if so, just add using system; - Because the Array class is located in the ‘system’ namespace.

Couple of notes for you:

  1. You’re already inside your class, you don’t need to access your members via this, you could just say waypoints instead of this.waypoints

  2. Why are you overriding Start and Update? - You don’t need to.

  3. You certainly don’t need Main here :))

  4. You don’t need to ‘manually’ use an enumerator, in fact, you should avoid it for now since you’re new to C#. Just load the components to a normal array (no need to use the Array class), like so

    Transform children = waypoint.GetComponentsInChildren < Transform > ();

and then when you wanna loop, you just use a for each loop, JS-style:

foreach(var child in children) if (child....) 
 // do your stuff...

It’s true that GetCompsInChildren return a Component, but a Transform ‘is a’ component :wink: (inheritance) that’s why the previous code is correct.

Im from a c++ background so I might be wrong :slight_smile:
but can you declare waypoints like : private int waypoints;