HELP How to remove gameobject from Built-in Arrays

basicaly i just want to know how do u remove a gameobject from a Built-in Arrays
if u dont know…its that

var BLA:GameObject;

i know ther are a lot of arrays that can be better but i tried and that one is better for what i want to do…so if u know…if u have reference…tell me PLZ!

2 Answers

2

You can’t do that easily. You need to use a List.< GameObject> if it needs to be added to or removed from while the game is playing.

You could do either of these: var newArray = new GameObject[BLA.Length-1]; for(var i = 0; i < BLA.Length-1; i++) newArray = BLA*;* BLA = newArray; This would remove the last item, you could change it to remove any old item with a bit of work. Nasty though - use a List! Also you could use Linq Import System.Linq; ... var thingToRemove = someGameObject; BLA = BLA.Where(function(g) g != thingToRemove).ToArray(); Again - you'd be better off using a List List.Remove(someGameObject);

–

CUZ I HAVE THE PERFECT INVENTORY SYSTEME......BUT THE THING IS THAT i cant remove thing...i realy need this

–

var newArray = new GameObject[InBag.Length-1]; var j = 0; for(var i = 0; i < InBag.Length; i++) { if(InBag != ITEM) newArray[j++] = InBag*;* } InBag = newArray;

–

It's late - I must have typed that script 6 times :D Should be right now...

–

You know what I'm gonna say though - an inventory system should really really use List - you've got to add things to it as well...

–

//remove item in slot 0
InBag.RemoveAt(0);

Everything slides up so what was in slot 1 now became slot 0.

Array functions list with examples at the bottom:

http://docs.unity3d.com/Documentation/ScriptReference/Array.html

Also if you have a inventory system you may want to use a empty object as placeholder instead of removing items. This way everything stays in place. instead of removing an item you replace it with an emtpy placeholder. var empty :gameObject; // assign a "empty" prefab item. //Clearing a slot: InBag[5]= empty; When checking if a slot is availeble you just need to check if it hold the empty prefab. if(InBag[5] == empty) { // Slot is availeble. }

–

yeah!!! right i'll keep that in mind in case...but the things is that the gameobject will be in the length and i want to remove it....exept if i can findgameObject by component...it will be great?

–

I know its an other question but i have an idea... supose i have this var BLA:GameObject[]; var BLA2:GameObject[]; i put an empty gameobject to replace the one i dont want...or delete......then by finding every gameobject with a script attach to it.....i put those inside the var BLA2 after resize it to 0 then i resize the var Bla to 0...so the empty gameobject will be delete...then i put back the objects inside the BLA2 in the var BLA...?

–

Not sure what you mean but you can use a for loop to iterate trough every object in the array and check that object for a sertain component or script value as shown in whydoidoit awnsers.

–

I seem to remember vaguely that there is an obscure built in array command that does RemoveAt, that eric5h5 said somewhere, but i forgot it. it's a strange syntax of RemoveAt i dunno. i need it now i can't remember it!

–