x


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?

more ▼

asked Nov 09 '10 at 11:37 PM

Dylan Cristy gravatar image

Dylan Cristy
155 9 10 19

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

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.

more ▼

answered Nov 09 '10 at 11:39 PM

Adam Rademacher gravatar image

Adam Rademacher
1.1k 1 3 19

That's kind of what I expected, but wanted to verify. Thanks!

Nov 09 '10 at 11:55 PM Dylan Cristy

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?

Jan 19 '11 at 03:13 PM schwertfisch

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

Jan 25 '11 at 03:29 AM Dylan Cristy
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x2087
x1363
x764
x116

asked: Nov 09 '10 at 11:37 PM

Seen: 1955 times

Last Updated: Nov 09 '10 at 11:37 PM