- Home /
[UNITY 2D] Can I create a spawner that spawns another enemy after its death using IF statements?
Will this work or would i need something else? I am taking a udemy course and the TA said that i can not use IF statements because it's not a number and I am dealing with gameobjects.
ALSO Will this work if i destroythe gameobject after a certain amount of time? at the moment my player is able to attack enemies.
public GameObjecct Enemy;
public float spawnTime;
private float timeReset;
private void Start ()
{
timeReset = spawnTime;
}
void Update()
{
spawnTime -= 2 * Time.detalTime;
if (enemyHealth <= 0)
{
Instantiate (Enemy, transform.position, Quaternion.identity);
timeReset = 0;
}
}
It depends what you are doing with the enemy GameObjects once they hit zero health. If you are destroying them just as an if statement to count the number of enemies and only spawn if you have less than the maximum.
public GameObjecct Enemy;
public float spawnTime;
private float timeReset;
private int $$anonymous$$axEnemies=10;
private int EnemyCount;
private void Start ()
{
timeReset = spawnTime;
}
void Update()
{
spawnTime -= 2 * Time.detalTime;
if (enemyHealth <= 0 && EnemyCount<$$anonymous$$axEnemies)
{
Instantiate (Enemy, transform.position, Quaternion.identity);
timeReset = 0;
EnemyCount+=1;
}
}
// In your code that destroys the enemies
EnemyCount-=1;
If you don't destroy them, you'll want to just adjust the $$anonymous$$axEnemies count at the point they hit zero health ins$$anonymous$$d. $$anonymous$$ake sure it's a trigger though, so they don't continually adjust the count whilst they remain at 0.
Did this help? If so, please mark as the best answer.
I can't mark this as anything it says reply =/ and up vote
What is enemyHealth
? You don't seem to have declared it anywhere and it's absolutely critical to following what you're doing here.
Assu$$anonymous$$g enemyHealth is an int then there's nothing obviously logically wrong with the code. But also not nearly enough information to be able to evaluate what it does.
Sorry, i've been busy. This code is just an idea of what im trying to do with the game i'm trying to make.
Yes, EnemyHealth is going to be an int.
In that case there doesn't appear to be anything wrong with it. You will need to get clarification from your TA because what you've told us that they've said, makes no sense whatsoever.
But if the code that you're showing us is not the code that prompted their comment then clearly you are already misrepresenting them. Why not show us that code?
Also, "i can not use IF statements because it's not a number" makes no sense at all.
The only data type that is guaranteed to appear when using an if statement is bool
(which is not a number), because the parentheses following the if have to contain something that can be resolved to a bool.
Are you sure that's what they said? I suspect there was more to it than that.
Answer by galactichyperstar · May 04, 2019 at 11:45 PM
Try this:
public GameObject EnemyPrefab;
public GameObject CurrentEnemy;
void Update() {
if (!CurrentEnemy) {
CurrentEnemy = Instantiate(EnemyPrefab, transform.position, Quaternion.identity);
}
}
It will spawn an enemy as soon as the one it spawned is destroyed.
I am taking a udemy course and the TA said that i can not use IF statements because it's not a number and I am dealing with gameobjects.
If your TA said this, they are wrong!
Your answer
Follow this Question
Related Questions
Animation of the death (2D Game) 0 Answers
Help with InstantDeath Collider Script? 1 Answer
How to stop enemy within certain distance of player? 2 Answers
Child of a player not syncing position with network? 0 Answers
Why is my character taking damage when attacking & why is it attacking multiple times per hit? 2 Answers