- Home /
How to Find all objects with Tag and List their Transforms?
Good day to you, I'm making a 2D Space Blaster game, and I'm currently working on enemy movement and path finding. I've created a bunch of Waypoint Markers on the level and tagged them "Waypoint".
Now what I want to do in the Enemy's Script, is to Find all these Waypoints by their tags, then add their Transform.position's to a Transform List/Array. So that later I could randomly select a waypoint position from this list and make it my Enemy's next destination.
I couldn't find any answers for this anywhere since no one else had asked such a question. So I tried checking different questions and trying to piece together their answers to get what I want but it didn't work. So I don't have any code to present here or anything. I'm empty handed.
If anyone could write this code or at lease explain to me how to make it, I would greatly appreciate it!
Answer by jmhoubre · Sep 24, 2021 at 09:16 PM
Hello,
I think the following code addresses your problem. The script is to be attached to an empty. This empty is to be referenced in your enemy controller, so that the latter can get a random waypoint (or you can add a singleton if you prefer).
using UnityEngine;
/// <summary>
/// Select a random waypoint.
/// </summary>
public class WaypointManager : MonoBehaviour
{
private Transform[] waypoints;
private void Awake ()
{
// Search for all waypoints. Warning, there is a 's' after Object.
GameObject[] gameObjects = GameObject.FindGameObjectsWithTag ("Waypoint");
// Initialize the array.
waypoints = new Transform[gameObjects.Length];
// Store the transforms.
for (int i = 0; i < gameObjects.Length; i++)
{
waypoints[i] = gameObjects[i].transform;
}
Debug.Log ($"{waypoints.Length.ToString ()} waypoint(s) found.");
}
/// <summary>
/// Returns the transform of a random Waypoint.
/// </summary>
public Transform GetRandomWaypoint ()
{
// Choose a random waypoint.
int randomRank = Random.Range (0, waypoints.Length);
return waypoints[randomRank];
}
}
Answer by AlgoUnity · Sep 24, 2021 at 06:43 AM
There's a lot of ways to do this, you can use FindGameObjectsWithTag("Waypoint").transform.position. You can also use FindObjectsOfType<\Waypoint\>().transform.position, where Waypoint is the name of the script you made for waypoints (if you did make one). Ideally you would want to avoid calling this function a million times and have an array in memory that holds these waypoint positions, and gets updated whenever a waypoint is created or destroyed. If you have Game Manager script (a single script that handles a bunch of the general game stuff), you can create a public Transform[] or public GameObject[] called waypoints and it will show up in the editor Inspector, where you can choose its size and drag all of the waypoint objects into the array. This is a good way of doing if you are creating the waypoints in the Scene and they always stay the same, you can to do it for each Scene.
Answer by bubzy · Sep 24, 2021 at 01:13 PM
Make a scriptable object list and add all the waypoints to it, every game object can access that list with no need to hunt for the other game objects
Answer by Chimz · Sep 25, 2021 at 07:22 PM
Thank you everyone! Your answers have been very helpful, so quick too.
Thank you AlgoUnity and Bubzy for the explaining a better way of implementing such a mechanic.
And special thanks to you jmhoubre, for the time and energy spent in both writing such a well script for this situation (with great optimization) and also explaining everything with Comments! I really needed the comments to understand what's happening (I'm not a pro programmer). Thank you! Best Answer goes to you.
Your answer
Follow this Question
Related Questions
Possible shader interference when updating matrix 1 Answer
Detecting OnMouseDown from colliders in an array 1 Answer
Remove object from List after gameObject was destroyed in between Scene loads 1 Answer
Trying to change character skins at the push of a button. C# 1 Answer
Need my function to work with different lists of different values (classes) 1 Answer