- Home /
trying to spawn enemies with different healths.
i am trying to spawn waves of enemies.each wave creates no of enemies that is one more than the previous one. when the no of enemies is 7 i want to reset the number of enemies back to one and increase health by twenty. but the health variable is not being updated. pls help.
this is my spawning code
GameObject newCharacter = Instantiate(spawnCharacter, spawnPosition, Quaternion.identity) as GameObject; newCharacter.transform.LookAt(planet.transform.position); newCharacter.transform.Rotate(-90, 0, 0); newCharacter.GetComponent().health=amount; } timer = 0;
}
timer += Time.deltaTime;
difficulty += multiplier * Time.deltaTime;
if(difficulty>=7)
{
difficulty = 1;
amount += 20;
}
and this my health code
{ // Start is called before the first frame update public float health = 20f; public GameObject htflsh; public void TakeDamage(float amount) { health -= amount; if(health<=0f) { Die(); } } void Die() { if (htflsh != null) { var hitvfx = Instantiate(htflsh, transform.position, Quaternion.identity); var pshit = hitvfx.GetComponent(); if (pshit != null) { Destroy(hitvfx, pshit.main.duration); } else { var pschild = hitvfx.transform.GetChild(0).GetComponent(); Destroy(hitvfx, pschild.main.duration); } Destroy(gameObject); } }
}
it looks like you use a variable called amount to do a lot of different things, not good practise. Would be better if you showed all your code. You can just do: if(difficulty<7) newCharacter.GetComponent().health=40; else newCharacter.GetComponent().health=60;