Count of gameobject not working
Hi, the below piece of code is meant to produce a single clone enemy when the number of enemies drops to zero. Instead, the code generates an infinite number of clones, and the countEnemy variable never drops to zero even when there are no enemies on screen or in the SampleScene window.
The enemies have the appropriate tag...
Any idea why?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemyPlacement : MonoBehaviour
{
public GameObject Enemy;
private GameObject Clone;
public static int countEnemy;
void Start()
{
Clone = Instantiate(Enemy, new Vector3(0, 0, 0), Quaternion.identity);
countEnemy = 1;
}
void Update()
{
Debug.Log("Count Enemy : " + countEnemy);
countEnemy = GameObject.FindGameObjectsWithTag("Enemy").Length;
if (countEnemy == 0)
{
Invoke("Replace", 1.0f);
countEnemy = 1;
}
}
void Replace()
{
Clone = Instantiate(Enemy, new Vector3(0, 0, 0), Quaternion.identity);
}
}
Answer by fellovicbeatz · Jan 10, 2020 at 07:16 PM
Update is called every frame, if the MonoBehaviour is enabled.
Your answer
Follow this Question
Related Questions
Weird FindObjectWithTag and Method-Access behaviour 1 Answer
Best way to reach a component or script's function? 1 Answer
How to put ".transform" after "GameObject.FindGameObjectsWithTag()" 1 Answer
Best alternative for GameObject.Find 2 Answers
How do i convert game object counts, into int, and put into string? 0 Answers