FindObjectWithhTag return only one GameObject

I have an array of GameObjects and I wanna get them with “FindGameObjectsWithTag” method.In my BaseEnemy class on start I change tag to “Enemy” and in result I get only one GameObject(instead of 3)

public class BaseEnemy : MonoBehaviour
{
    protected float max_speed;
    [SerializeField]
    protected float speed;

	protected float max_hp;
    [SerializeField]
	protected float hp;

    protected float max_damage;
    [SerializeField]
    protected float damage;

	protected Rigidbody2D rb;
	protected Controller controller;
	public GameObject enemy;

    private void Start ()
    {
        enemy.tag = "Enemy";
		controller = GameController.FindObjectOfType<Controller> ();
		rb = GetComponent<Rigidbody2D> ();
        
        hp = max_hp;
        speed = max_speed;
        damage = max_speed;
    }
}
public class GameController: MonoBehaviour
{

    public GameObject[] enemies;

	private void Start ()
    {
        enemies = GameObject.FindGameObjectsWithTag("Enemy");
    }
}

My guess is that all three of the enemies in your scene have a reference to the same enemy in public GameObject enemy;

I would try doing something like this to test:

 private void Start ()
         {
             //Set the tag of the GameObject this script is assigned to
             transform.GameObject.tag = "Enemy";
             controller = GameController.FindObjectOfType<Controller> ();
             rb = GetComponent<Rigidbody2D> ();
             
             hp = max_hp;
             speed = max_speed;
             damage = max_speed;
         }

I think the issue is using Start() for setting up the BaseEnemy. It’s possible the GameController hits it’s Start() first, thus doesn’t find anything or one, etc.


For fun, change your Start() in BaseEnemy to Awake(). Since you are really just setting up your BaseEnemy object, it makes more sense to do Awake() here. And then you won’t have a potential race condition with the two scripts.