- Home /
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 :(
I converted to C# using .NET Refactor, and it didn't go well i know.
I'm very confused, can you at least give me a working script? or at least a part of it so i can figure out my way with the rest.
As i said, i never used C#, and it's urgent , please?
Yes, I can help. Did you look at my solution. What errors are you having now?
You need to be
using System.Collections
You also need to declare your int array like:
int[] waypoints;
Please check the answers when you get a chance, accept the answer that helps, if one of them does.
Answer by vexe · Aug 13, 2013 at 10:09 AM
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:
- You're already inside your class, you don't need to access your members via - this, you could just say- waypointsinstead of this.waypoints
- Why are you overriding - Startand- Update? - You don't need to.
- You certainly don't need - Mainhere :))
- 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 (); 
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 ;) (inheritance) that's why the previous code is correct.
Answer by KaffeSumpen · Aug 13, 2013 at 10:25 AM
Im from a c++ background so I might be wrong :) but can you declare waypoints like : private int[] waypoints;
Your answer
 
 
             Follow this Question
Related Questions
convert string to array to write to document 0 Answers
Trying to convert a JC script to C # 2 Answers
Help converting this to C# - a few issues 3 Answers
C# to UnityScript conversion help. 0 Answers
Obtaining a Array with Unique Items 1 Answer
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                