- Home /
HELP Find gameObject With tag in another array
those are my vars...
var InBag:GameObject[];
var InBag2:GameObject[];
so i want to find every game object with a certain tag inside the
var InBag:GameObject[];
and add it to the
var InBag2:GameObject[];
here im with my script
var InBag:GameObject[];
var InBag2:GameObject[];
function Refresh()
{
for(InBag2: GameObject in InBag)
InBag2= GameObject.FindGameObjectsWithTag("Object");
}
Answer by GuyTidhar · Mar 28, 2013 at 01:55 PM
'FindGameObjectsWithTag' searches through all the active game objects in the scene, not through a specified list.
Therefor you need a dynamic list in memory (dynamic in the sense that you dynamically and automatically in Unity's case assign more memory for your array items as you go along adding more to your list).
import System.Collections.Generic; // Don't forget to place this at top of file!
var InBag:GameObject[];
var InBag2:GameObject[];
// Update the refresh function as follows
function Refresh()
{
// Prepare a temporary place in memory to add GameObjects to
var temp : List.<GameObject> = new List.<GameObject>();
// Go through all GameObjects in InBag
for(var inBag : GameObject in InBag)
{
// Does the tag of the current GameObject 'inBag' equal "Object"?
if ( inBag.CompareTag("Object") )
temp.Add(inBag); // It does - so add it to the temporary list
}
// Now take all existing GameObjects from the temporary list and export it to a regular built-in array called 'InBag2'
InBag2 = temp.ToArray();
}
actually it work....but the things is that...this is for another trick too...because using list system...doesnt match with my type of inventory system...so because i cant remove stuff from my inventory without using list syteme...i tried to refresh my InBag...so i replace the deleted object by a untagged object....and i want to add all the gameobject with OBJECT tag on it to an other than reset my INBag and add InBag2 to InBag...here is my real script...i didnt want it to be so complicated but doesnt work..
//////////Object bag
var TargetObject:Transform;
//////////bag settings
var Capacity:int=100;
var InBag:GameObject[];
var InBag2:GameObject[];
function Ad(){InBag+=[TargetObject.gameObject];}
function Refresh()
{
for(InBag2: GameObject in InBag){ 'eror line'
InBag2= GameObject.FindGameObjectsWithTag("Object");
}
InBag=new GameObject[0];
for(var i = 0; i < InBag2.length; i++){InBag+=[InBag2[i]];}
}
1.
This line:
GameObject.FindGameObjectsWithTag("Object");
Searched all the game object of your scene, and not only within InBag. Judging from what you wrote that is not what you need.
I'd advise you to keep using my suggestion.
2.
What else do you wish to do with your inventory system?
You can still use List, but I'll need to know what you wish to do with it.
$$anonymous$$y suggestion only uses the List collection in order to temporary hold your required items.
Do not remove:
var InBag:GameObject[];
var InBag2:GameObject[];
(I have now edited my answer to show these two lines).
3.
'for(InBag2: GameObject in InBag)'
Is not the correct syntax.
You should be doing:
for(var singleItemFromInBagInventroy : GameObject in InBag)
{
// singleItemFromInBagInventroy is a game object within InBag
}
Soppose i use ur first script...how do i put back the stuff inside InBag? cuz its not a ''real array'' compare to the InBag2...?
When are you removing anything from InBag?
InBag and InBag2 are both built-in GameObject arrays.
What do you need to "put back" into InBag?
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Array weapons Wheel scroll switch 1 Answer
Array problem? help please! 1 Answer