- Home /
Spawn enemy while player is alive....
I am trying to spawn enemies while the player is still alive, however nothing is being spawned at all. Any Ideas why? I am new to programming so if it is a simple fix please forgive me :)
Here is my code
public class CallSpawn : MonoBehaviour
{
public Transform astroid;
public Vector3 position;
public int count = 0;
public void Spawn()
{
while (GameMaster.isDead == 0)
{
count = count + 1;
if (0 == (count % 10))
{
position = new Vector3(Random.Range(-30.0F, 30.0F), 20, Random.Range(0.0F, 0.0F));
Instantiate(astroid, position, Quaternion.identity);
}
}
}
}
Class being called
using UnityEngine;
using System.Collections;
public class GameMaster : MonoBehaviour {
public static int isDead = 0;
public static void KillPlayer(Player player)
{
Destroy(player.gameObject);
isDead = 1;
}
public static void KillEnemy(Enemy astroid)
{
Destroy(astroid.gameObject);
}
}
Answer by tpusch · May 10, 2016 at 04:27 PM
The biggest problem I see is that your spawn function is an infinite loop, after entering the while GameMaster.isDead can never change from 0. Also my guess is that you are trying to use your count as a delay timer between spawns, if that is the case you can put something like this in the CallSpawn update and never have to worry about manually calling spawn.
private float spawnDelay = 10;
private float timeUntilSpawn = 0;
void Update(){
if(GameMaster.isDead == 0){
if(timeUntilSpawn <=0{
position = new Vector3(Random.Range(-30.0F, 30.0F), 20, Random.Range(0.0F, 0.0F));
Instantiate(astroid, position, Quaternion.identity);
timeUntilSpawn = spawnDelay
}else{
timeUntilSpawn -= Time.deltaTime;
}
}
}
One other note to help readability would be to change isDead from an int to a bool if you only have dead or alive.
public static bool isDead = false;
public static void KillPlayer(Player player){
isDead = true;
}
And then you don't have to worry about comparing to 1 or 0 in your checks with it, simply if(GameMaster.isDead)
or if(!GameMaster.isDead)
Okay thank you! this does work but the clones do not kill me like the parent does.... dimple fix, or do i need to scrap something and restart?
Probably that astroid, asteroid?, should be a GameObject rather than a Transform. Which I missed earlier.
Okay, I changed them to GameObjects but I have to change my Player kill code now too. I looked up the code (I was using distance formula) but the code I found on the wiki did not work. Sorry if this is a very simple problem. I'm new to program$$anonymous$$g and even newer to C# and Unity. Here is the code (code that is commented out is the old method I was using)
using UnityEngine;
using System.Collections;
using System;
public class Player : $$anonymous$$onoBehaviour
{
public GameObject player;
public GameObject astroid;
public int fallBoundary = -10;
void OnCollisionEnter2D(Collision2D coll)
{
if (coll.gameObject.tag == "astroid")
{
Game$$anonymous$$aster.$$anonymous$$illPlayer(this);
}
}
}
//public class Player : $$anonymous$$onoBehaviour
//{
// public Transform player;
// public Transform astroid;
// public int fallBoundary = -10;
// public void Update()
// {
// if ((player != null) && (astroid != null))
// {
// if (transform.position.y < fallBoundary)
// {
// Game$$anonymous$$aster.$$anonymous$$illPlayer(this);
// }
// if ((player.position.x == astroid.position.x) && (2 > (astroid.position.y - player.position.y)))
// {
// Game$$anonymous$$aster.$$anonymous$$illPlayer(this);
// }
// else if ((player.position.y == astroid.position.y) && ((2 > (astroid.position.x - player.position.x)) || (2 > (astroid.position.x - player.position.x))))
// {
// Game$$anonymous$$aster.$$anonymous$$illPlayer(this);
// }
// else if (2 > ($$anonymous$$ath.Sqrt($$anonymous$$ath.Pow(player.position.x - astroid.position.x, 2) + $$anonymous$$ath.Pow(player.position.y - astroid.position.y, 2))))
// {
// Game$$anonymous$$aster.$$anonymous$$illPlayer(this);
// }
// }
// }
//}
Answer by JoshDangIt · May 10, 2016 at 04:55 PM
You'll want something more like this: (Untested)
public class CallSpawn : MonoBehaviour
{
public GameObject astroid; //this should be a GameObject rather than a transform.
public float spawnRate = 10f; //enemy spawn rate in seconds
private bool shouldSpawn = false;
private bool nextSpawnTime;
public void StartSpawning()
{
nextSpawnTime = Time.time + spawnRate;
shouldSpawn = true;
}
void Update() //update is called once each frame
{
if(shouldSpawn == false) return; //do not run the below code if the enemies shouldnt spawn
if(GameMaster.isDead == true) //replace GameMaster.isDead with a bool. It's much cleaner.
{
shouldSpawn = false;
}
if(Time.time > nextSpawnTime)
{
Vector3 position = new Vector3(Random.Range(-30.0F, 30.0F), 20, Random.Range(0.0F, 0.0F));
Instantiate(astroid, position, Quaternion.identity);
nextSpawnTime += spawnRate;
}
}
}
Okay, I tried both methods of the suggestions I got, both work. But my clones of the parent do not kill me (I'm using distance formula). Any ideas why?
Based on the other comment you made, I think you'll want to make a different script to put on the asteroid that kills the player from there ins$$anonymous$$d (It would be much better practice). Have you watched any guides on creating and instantiating prefabs?
Your answer
![](https://koobas.hobune.stream/wayback/20220612080454im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
instanstiate Spawns more then one objec 0 Answers
Enemies spawn on top of each other(C#)(Unity) 1 Answer
Increasing spawn rate over time 1 Answer
Network spawning 1 Answer