1

I'm working to translate a Unity project originally in UnityScript to C#. I already have translated a good part of the project, but I'm confronted with some problems:

The first problem is linked with GetComponent. I have a file EnnemyController.cs with a lot of functions for get/set enemies. But before all of this function, I need to initialize the characterController.

private CharacterController characterController;
characterController = GetComponent<CharacterController>();

You can see the context here : http://pastebin.com/U09aH4ZA. If I add the code above in a function It's working but not in the class... Returned to me the following error:

UnityEngine.Component.GetComponent (string) is a method but is used as a type EnemyController.characterController is a field but is used as a type

The second problem is linked with array in C#. More precisely with array of array, the UnityScript is something like this:

var connected : Array = Array ();
static var waypoints : Array = Array ();
var objects : Object [] = FindObjectsOfType ( AutoWayPoint );
waypoints = Array ( objects );

I have no idea how to translate this kind of thing, and you can see the whole file here: http://perche.jeremy.free.fr/sup_scr/AutoWayPoint.js

And for finish, I wanted to know the difference between IEnumerator and IEnumerable.

user3071284
  • 6,955
  • 6
  • 43
  • 57
PokeRwOw
  • 609
  • 1
  • 6
  • 29

1 Answers1

2

seems like your first error is because you are calling GetComponent<CharacterController>() outside of any method, try to do this on your Start() method.


You can translate your js Arrays in a simple c# array (like int []arrayOfInts = new int[20];) or use something like ArrayList if you are not sure of the amount of elements that the array will have. Now, you have to determine which kind of elements will be used.
And you last question is already answered here: What is the difference between IEnumerator and IEnumerable?
Community
  • 1
  • 1
mayo
  • 3,845
  • 1
  • 32
  • 42
  • Yeah GetComponent() is called outside of any method because I want to access to this element anywhere in the class. So if I call this in Start(), I can access to this element anywhere? For the array, the question was more linked with array of array = multidimensional array ? – PokeRwOw Sep 21 '15 at 20:14
  • If you want to access anywhere the element should be declared outside of your method, but if you also want to access to the element anytime you should get the reference before do anything, here you have 2 options; 1.- Declare the element as public in code and drag&drop the element from Hierarchy to your script in Inspector (ie; https://www.youtube.com/watch?v=TnJhhz120js ), or 2.- get the reference by code inside a method, in this case you can do that inside Start or Awake using GetComponent./// Syntax for mult dim array on c#: https://msdn.microsoft.com/en-us/library/2yd9wwz4.aspx – mayo Sep 21 '15 at 20:32
  • Thanks for your answers guy, this helped me alot ! :) – PokeRwOw Sep 21 '15 at 21:48