- Home /
correct method for clearing a gameObject array
Hi. What is the correct accepted method of clearing an array of gameObjects ?
e.g.
var arrayPathMarker : GameObject[] = new GameObject[100];
for (var i = 0; i< 100; i++) {
Destroy (arrayPathMarker[i]); // this , or the next command ?
// arrayPathMarker[i]=null;
}
Answer by Eric5h5 · Mar 26, 2012 at 02:37 PM
Do you want to just clear the array, or destroy the objects? Setting the entries in the array to null will clear the array, but won't destroy the objects. (Although you can just use System.Array.Clear.) Using Destroy will remove the objects, and the entries will be null. GameObjects are by reference.
I did want to Destroy the objects, and now knowing I can set to null without destroying the object is a bonus. Thankyou.
I just did this System.Array.Clear.(foundwalls);
And it said system.array.clear is not a generic definition-it's in JavaScript, how should I write it?
Function names can't have a "." after them. System.Array.Clear (foundwalls)
thanks, it still said
The best overload for the method 'System.Array.Clear(System.Array, int, int)' is not compatible with the argument list '(UnityEngine.GameObject[])'.
this one runs, although in JavaScript it seems to have an error, it prints the same array.length
System.Array.Clear(foundwalls,0,foundwalls.length);
print( "clr"+foundwalls.length);//no difference
this also
foundwalls = new GameObject[0];
print( "clr"+foundwalls.length);
-sorry I was getting an error because I was forgetting to clear the array counting variable as well as the array.
Right, you need to specify the index and the length. It's not supposed to change the length of the array; it just clears the contents. Please read the docs...arrays have a fixed size. If you want to change the size, use List ins$$anonymous$$d of an array.
Answer by Mastasytha · Feb 23, 2014 at 12:01 PM
I have found a method that works fine with me it checking to see if the game object is null here is my spawn script were i have implemented my code were i destroy the spawn point after it is used
public var Respawn : GameObject[];
function Start () { Respawn = GameObject.FindGameObjectsWithTag("Respawn");
}
function Update () { } function rs() { var spawn = Random.Range(0,Respawn.Length); if(Respawn[spawn] == null) { rs(); }else{ transform.position = Respawn[spawn].gameObject.transform.position; Debug.Log(spawn); transform.position.y = 2; Destroy(Respawn[spawn].gameObject); }
}
Answer by Mastasytha · Feb 23, 2014 at 01:53 PM
this is the code i used in my script to fix my errors with null game objects in an array
pragma strict
public var Respawn : GameObject[];
var spawnpoint : GUIText;
function Start () {
Respawn = GameObject.FindGameObjectsWithTag("Respawn");
}
function Update () {
}
function rs()
{
var spawn = Random.Range(0,Respawn.Length);
if(Respawn[spawn] == null)
{
rs();
}else{
transform.position = Respawn[spawn].gameObject.transform.position;
Debug.Log(spawn);
transform.position.y = 2;
Destroy(Respawn[spawn].gameObject);
}
}
Answer by skylem · Jan 10, 2015 at 08:58 AM
There is one alternative to this i thought im not sure how useful it will be to others but has helped me plenty so here it is, You can create a list based from your array, using a foreach to add each gameobject to your list, i feel lists are easier to use and clear especially so for anyone who is new to scripting, if anyone would like a step by step for this just email me Skylem@live.com.au and i'll provide a blow by blow of how to use a list aswell as an example.