plz do not tell me learn JS >>> only give me the answer …
I can or not >>>
if I can >>> how to do it >>
if doesn’t >>> that mean the JS is not powerful like C# >>> is this right
… I try many times and give me error
‘Add’ is not a member of ‘UnityEngine.GameObject’. or
‘Add’ is not a member of ‘UnityEngine.Transform’.
…
I have only quickly tested this, but I think it is what you need. If you run into this kind of problem again, it’s far quicker for you to post the code that you do have and does not work. To answer you I had to follow along the tutorial and write JS whilst the dude was writing c# which took me around 7 minutes. Possibly I could have corrected your code in 30 seconds.
import System.Collections.Generic;
var targets : List.<Transform>;
function Start () {
targets = new List.<Transform>();
AddAllEnemies();
}
function Update () {
}
function AddTarget( t : Transform) {
targets.Add(t);
}
function AddAllEnemies() {
var gos : GameObject[];
gos = GameObject.FindGameObjectsWithTag("Enemy");
for (var go : GameObject in gos) {
AddTarget(go.transform);
}
}
In any case, the specific problem you are trying to solve here, is that arrays in C# are always static, and cannot change their sizes. You should be using generic lists, instead!
// You need to put this line at the top of the file
// near the UnityEngine and System.Collections bits-
using System.Collections.Generic;
then, when you would define the array-
// instead of Transform[] name = new Transform[length];
List<Transform> transforms = new List<Transform>();
Then, when you want to add a member to it,
transforms.Add(newTransform);
You can iterate through that like this-
foreach(Transform trans in transforms)
{
// do some operation on trans
Debug.Log(trans.position);
}
You can delete specific transforms out of the list if you need to-
transforms.Remove(someTransform);
this will remove that transform from the list.
Remember, the ‘Generic’ in generic list means you can do this for any type! Just replace Transform in the type declaration with whatever type you need.