So I have multiple objects with the same name and tag, but with different coordinates. How can I add only only version of each game object to a list? My current code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Adder : MonoBehaviour {
public GameObject temp;
[SerializeField]
public List<GameObject> spawners = new List<GameObject>();
void FixedUpdate() {
temp = GameObject.FindGameObjectWithTag("SpawnPoint");
Debug.Log(temp);
spawners.Add(temp);
}
}
Hi @JackAshwell, liet’s get this straight. Let’s say you have 4 spawn point game objects, two named GA and two named GB?
then it is relatively easy to use list.Find to see if an object with temp.name is already in the list. If list.Find returns null, then you know it does not exist.
So for example:
if( ! temp.Find( lambda => lambda.name == "G4") ) ///not in the list, so add code here
However, you’re code is a little weird because it seems every update you are potentially going to get the same object - the first object found using FindObjectWithTag.
Also, you should not use FindObjectWithTag at all because it is slow. It is much better already have all game objects in a list by adding them when you spawn them, or by dragging them to public variables in the inspector and then adding them to a list of all such objects. You could then pull one out at random and add it to your spawners list
What are you trying to do exactly - what is the end purpose of this code?