I get an error ('RemoveAt' is not a member of (UnityEngine.GameObject) if I do this:
var nodes: GameObject[];
var tagName: String; // tag to find objects'
function Start()
{
nodes = GameObject.FindGameObjectsWithTag(tagName);
nodes.RemoveAt(1);
}
What is the syntax to add or remove a GameObject from a array of GameObjects?
This is in Javascript.
duck
3
You're getting this error because there are more than one kind of array which you can use in Unity, and they each have different functions and features.
There are built-in arrays, which are fixed-size, and don't have functions which would change their length (like Add, Remove). Built-in arrays are declared using square brackets.
There is also Unity's Javascript Array class, which does have these features - however none of Unity's functions which return collections of objects return Javascript Array objects, they all return built-in arrays!
(there are also many other array-like types available to use, but that's a different topic!)
So if you need to add or remove objects from your collection, generally you would retrieve the built-in array from your unity function, and convert it to a Javascript Array immediately after, like this:
var nodes : Array;
var tagName: String; // tag to find objects
function Start()
{
// get fixed array of node objects, store in a temp var:
var nodesFixedArray = GameObject.FindGameObjectsWithTag(tagName);
// convert to dynamic array
nodes = new Array(nodesFixedArray);
// now we can add/remove!
nodes.RemoveAt(1);
}
That's not possible, but you can convert built-in arrays to dynamic arrays like this:
nodesArray = new Array(nodes);
and back to static arrays like this:
nodes = nodesArray.ToBuiltin(GameObject);