- Home /
Find Active gameobject with tag and store in a List/Array as a Transform?
Right now I have an array of GameObjects that all can individually become active or inactive based on player level progression using playerprefs. The active GameObjects in the array are not in order, so I need to find a way to store only the active GameObject's positions so the player can travel between the available levels.
I have this at the moment:
void UpdateListOfWorlds()
{
GameObject[] availableWorlds = GameObject.FindGameObjectsWithTag("World");
foreach (GameObject world in availableWorlds)
{
if (world.activeInHierarchy == true)
{
Debug.Log("Number of active Worlds: " + availableWorlds);
}
}
}
This returns a number of logs equal to the number of active objects in the scene when UpdateListOfWorlds is used, which is good. But I'm unsure of how to actually get each object's transform position and store that in an array that the player can travel between...if that makes sense?
Answer by fblast1 · May 06, 2020 at 12:51 AM
you can grab that in array of the same size (or arraylist with add function) as the world and make it of Type Transform or Vector3 and assign that array in each iteration you already doing
if (world.activeInHierarchy == true)
{
Debug.Log("Number of active Worlds: " + availableWorlds);
worldTransformsArr.add(world.transform.position);//Arraylist Positions
worldTransformsArr.add(world.transform);//Arraylist of transforms
//use either one of them as needed
}
Regards
Thank you! This method worked out, too! I'll probably end up using this way so that I don't have to jump through any more hoops later down the line.
Answer by 12toe · May 06, 2020 at 12:55 AM
Shortly after, I realized that
GameObject[] availableWorlds = GameObject.FindGameObjectsWithTag("World");
could be set to
public GameObject[] availableWorlds;
void UpdateListOfWorlds()
{
availableWorlds = GameObject.FindGameObjectsWithTag("World");
}
And from there I can see that objects are being added to an array, in the exact order that they were unlocked, which is great! Now it's just a matter of calling a separate function that only allows travel between these GameObjects.
Answer by Riiich · May 06, 2020 at 12:59 AM
Use another list?
void UpdateListOfWorlds()
{
GameObject[] availableWorlds = GameObject.FindGameObjectsWithTag("World");
private List<GameObject> activeWorlds = new List<GameObject>();
foreach (GameObject world in availableWorlds)
{
if (world.act == true)
{
activeWorlds.Add(world);
}
}
}
Your answer
Follow this Question
Related Questions
Copying lists cross-class not working correctly 1 Answer
Custom Inspector: For every new element in one array create a brand new array? 0 Answers
Inventory String error 1 Answer
Connect object's position to shuffled array 1 Answer
How to enable object by clicking on the button through the Massive(array)? 0 Answers