For example, there are four monsters named ‘stupidy’ in the scene named ‘A’.
I have to detect the count of those monsters because if the player kill that monster the managing system should make the monster respawned in the field.
So if the count<4, monster respawn trigger will happen.
In this situation, how can i get the count of monsters by the variable and script? I need help a lot
You could do this in several ways. Store your monsters to some list when you create them, like a static list in monster class, or a list in some manager class.
Or find your monsters when you need to check your monster count. These links will help you in this case:
Note that using these find commands will most likely be slower than checking some list where your monsters are already added to / removed from.
BTW - this is a scripting question, not a 2D question, you should ask this in scripting forum instead.
“How can i use 'die’and ‘make it’ in unity c# code? I mean… the method or keywords.”
You could do something like this - your enemy collides with a bullet or player or whatever, you could use trigger collision as well, but here on collision is used, and enemy gets destroyed when it collides with player:
public class Enemy : MonoBehaviour
{
// your enemy count
public static int enemyCount = 0;
private void Awake()
{
enemyCount++;
}
private void OnCollisionEnter2D(Collision2D other)
{
if (other.gameObject.tag == "Player")
{
Destroy(this.gameObject);
UpdateEnemyCount();
}
}
public void UpdateEnemyCount()
{
enemyCount--;
// your condition here
if (enemyCount <= 0)
{
Debug.Log("All enemies are dead");
// call some method here
}
}
}
Instead of having enemy count in enemy class itself (shared between all enemies as it is static) you could have it in some manager class, then it is easier as you can see it in your inspector, where static fields won’t show up by default.