- Home /
If I delete an object stored in an array, will it shorten the array or leave an index position with a null value?
If I have an array of gameobjects (used to quickly iterate through those specific objects to enable/disable them, index positions don't matter), and I destroy one of the gameobjects through some means other than Array.RemoveAt
, will the array automatically shorten itself, or will it leave a hole in the array by having the array index where that gameobject was refer to null?
As a side question, would Array.RemoveAt
actually destroy the gameobject referred to at that index, or would it just remove the reference to it from the array?
Answer by Adam Rademacher · Nov 09, 2010 at 11:39 PM
The array and the object are completely separate. Destroying the gameobject will leave a null value in the array. RemoveAt will remove it from the array, but not from the game. You'll have to do both manually.
That's kind of what I expected, but wanted to verify. Thanks!
Found that question& answer useful too. Just to be sure Adam: "RemoveAt will remove it from the array" means it will leave a null value at its index or it will shorten the array's lenght?
Based on the example they give for RemoveAt in the scripting reference, I'm pretty sure it will shorten the array's length. http://unity3d.com/support/documentation/ScriptReference/Array.RemoveAt.html
since RemoveAt will shorten the array's length, will values in the array be repositioned?
Answer by fardinnn · Aug 08, 2013 at 12:43 PM
I test it ' game object has removed but array length and its content has no change . that means object in hierarchy and scene removed but in array has it. bellow the test script :
pragma strict
ar arr : Array ; var box : GameObject ; function Start () { arr = new Array(); for(var ii=0 ; ii<10 ; ii++){ arr.Add(GameObject.Find(("box"+ii))); } Debug.Log("0 : "+arr.length); for(ii=0 ; ii
Your answer
Follow this Question
Related Questions
using Contains(gameObject) to find and destroy a gameObject from a list 2 Answers
Remove an object from an array and destroy it (C#) 2 Answers
How can I destroy objects and instantiates new in Array? 2 Answers
deleting self-gameobject,deleting self-gameobject 0 Answers
how do i create a game object array with gameobjects of multiple tags 1 Answer