Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by SohaibTheGame · Aug 13, 2013 at 09:39 AM · javascriptarrayconvertnamespace

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 :(

Comment
Add comment · Show 4
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image SohaibTheGame · Aug 13, 2013 at 10:13 AM 0
Share

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?

avatar image vexe · Aug 13, 2013 at 10:30 AM 0
Share

Yes, I can help. Did you look at my solution. What errors are you having now?

avatar image clunk47 · Aug 13, 2013 at 05:27 PM 2
Share

You need to be

using System.Collections

You also need to declare your int array like:

int[] waypoints;

avatar image clunk47 · Aug 14, 2013 at 09:57 PM 0
Share

Please check the answers when you get a chance, accept the answer that helps, if one of them does.

2 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

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:

  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 ();

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Magiichan · May 10, 2015 at 04:43 PM 0
Share

He used a decompiler to translate the code -_-

avatar image
0

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;

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

18 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges