- Home /
Lists, Arrays and c# equivilent of js Array()
Hi, I am trying to convert the way point script from the FPS tutorial script that is written in java script in order to implement it within my own enemy AI. I am having trouble where they use the Array class to manage arrays. In c# I assume you would use a list to get similar functionality. However they use the following code:
//global array variables
static var waypoints = Array();
var connected = Array();
//assigning the way points array to store all the current way points
var objects : Object[] = FindObjectsOfType(AutoWayPoint);
waypoints = Array(objects);
I can get the functionality using lists but the FindObjectsOfType() doesn't return a list. The other option is to avoid containers and use generic arrays which works much better until when creating the connected array they use the array.Add() which isnt supported by generic arrays unless I code it myself.
Anyone know the best way to go about doing something like this?
I think this reference is what you're after :
http://wiki.unity3d.com/index.php?title=Which_$$anonymous$$ind_Of_Array_Or_Collection_Should_I_Use?
This link has examples for JS and C#, making it very handy for converting between JS and C#.
For Arrays that remain the same size, use inbuilt arrays. For Arrays that change in size (as your waypoints could), use List.
Answer by bompi88 · Dec 09, 2012 at 02:15 PM
If your intentions are not to edit the array,
Object[] waypoints = FindObjectsOfType(typeof(AutoWayPoint)) as Object[];
This could actually replace the two last lines of your code.
Your answer
Follow this Question
Related Questions
Add GameObjects to a list using OnTriggerEnter 0 Answers
Grabbing GameObjects and putting into Transform array? 1 Answer
How to modify array values? 1 Answer
Serialization Error 1 Answer