- Home /
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!
Answer by whydoidoit · Mar 27, 2013 at 02:07 AM
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[i] = BLA[i];
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);
i do understand but im lost...this is my script...how do u gonna do the first one in it....
this is on a GUI function.....how do u expect it....
> for(var x=0;x <Inbag.Length;x++)
{
var ITE$$anonymous$$= InBag[x];
GUILayout.BeginHorizontal();
GUILayout.Button(ITE$$anonymous$$.GetComponent(InItems).Icon);
GUILayout.BeginVertical();
GUILayout.Box(ITE$$anonymous$$.name);
GUILayout.BeginHorizontal();
if(GUILayout.Button("Use"))
{
" here when i click here i want to remove it from the thing"
}
GUILayout.EndHorizontal();
GUILayout.EndVertical();
GUILayout.EndHorizontal();
GUILayout.EndArea();
CUZ I HAVE THE PERFECT INVENTORY SYSTE$$anonymous$$$$anonymous$$.....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[i] != ITE$$anonymous$$)
newArray[j++] = InBag[i];
}
InBag = newArray;
It's late - I must have typed that script 6 times :D Should be right now...
Answer by Bluestrike · Mar 27, 2013 at 08:26 AM
//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 ins$$anonymous$$d of removing items. This way everything stays in place.
ins$$anonymous$$d 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 $$anonymous$$d 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!
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Array Help GameObject Length Inventory 1 Answer
Array problem? help please! 1 Answer
Array weapons Wheel scroll switch 1 Answer