I want to store some gameobjects on a list using FindGameObjectsWithTag method. Currently i am creating a temporary array to return from the method, and then copy each elements individually.
Is there any other way where I don’t need to create a temporary array?
Thanks.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Test : MonoBehaviour
{
private GameObject[] enemy;
public List<GameObject> enemies;
void Start()
{
enemy = new GameObject[10];
}
void Update()
{
if (Input.GetButtonDown("Fire"))
{
Debug.Log("Spawning...");
FindEnemy();
}
}
void FindEnemy()
{
enemy = GameObject.FindGameObjectsWithTag("Enemy");
foreach (GameObject e in enemy)
{
enemies.Add(e);
}
}
}