Skip to main content
added 9 characters in body
Source Link
Philipp
  • 123.2k
  • 28
  • 264
  • 345

As ThePumkinMelon suggested, create a prefab for your player game object.

Then remove the player object from your scene, have your player spawner reference the player prefab and also reference the current player game object in the scene:

public class PlayerSpawner : MonoBehaviour {

    public GameObject playerPrefab;
    private GameObject currentPlayer;

Then in the Update-method of the PlayerSpawner, do this:

    if (currentPlayer == null) {
         currentPlayer = Instantiate(playerPrefab, transform.position, transform.rotation);
    }

This will check if its player is in the scene, and when it is not it will spawn a new player at the position and with the rotation of the spawner (that means you need to place the PlayerSpawner game object at the location where you want the player to spawn).

There are two cases where currentPlayer == null will be true.

Now your Dead coroutine just needs to Destroy the player game object and the PlayerSpawner will promptly instantiate a new one.


However, keep in mind that if you excessively destroy and instantiate a lot of game objects, you might start to notice notable performance degregation due to memory fragmentation and garbage collection. In this case this shouldn't matter, because the player will die every few seconds at most, even if you create a hellishly difficult game. But if you have some kinds of objects in your game which get destroyed and respawned serveral times per second, then you might want to look into a pattern called "object pools". But that's a topic for a different question.

I am looking forward to playing your game.

As ThePumkinMelon suggested, create a prefab for your player game object.

Then remove the player object from your scene, have your player spawner reference the player prefab and also reference the current player game object in the scene:

public class PlayerSpawner : MonoBehaviour {

    public GameObject playerPrefab;
    private GameObject currentPlayer;

Then in the Update-method of the PlayerSpawner, do this:

    if (currentPlayer == null) {
         currentPlayer = Instantiate(playerPrefab, transform.position, transform.rotation);
    }

This will check if its player is in the scene, and when it is not it will spawn a new player at the position and with the rotation of the spawner (that means you need to place the PlayerSpawner game object at the location where you want the player to spawn).

There are two cases where currentPlayer == null will be true.

Now your Dead coroutine just needs to Destroy the player game object and the PlayerSpawner will instantiate a new one.


However, keep in mind that if you excessively destroy and instantiate a lot of game objects, you might start to notice notable performance degregation due to memory fragmentation and garbage collection. In this case this shouldn't matter, because the player will die every few seconds at most, even if you create a hellishly difficult game. But if you have some kinds of objects in your game which get destroyed and respawned serveral times per second, then you might want to look into a pattern called "object pools". But that's a topic for a different question.

I am looking forward to playing your game.

As ThePumkinMelon suggested, create a prefab for your player game object.

Then remove the player object from your scene, have your player spawner reference the player prefab and also reference the current player game object in the scene:

public class PlayerSpawner : MonoBehaviour {

    public GameObject playerPrefab;
    private GameObject currentPlayer;

Then in the Update-method of the PlayerSpawner, do this:

    if (currentPlayer == null) {
         currentPlayer = Instantiate(playerPrefab, transform.position, transform.rotation);
    }

This will check if its player is in the scene, and when it is not it will spawn a new player at the position and with the rotation of the spawner (that means you need to place the PlayerSpawner game object at the location where you want the player to spawn).

There are two cases where currentPlayer == null will be true.

Now your Dead coroutine just needs to Destroy the player game object and the PlayerSpawner will promptly instantiate a new one.


However, keep in mind that if you excessively destroy and instantiate a lot of game objects, you might start to notice notable performance degregation due to memory fragmentation and garbage collection. In this case this shouldn't matter, because the player will die every few seconds at most, even if you create a hellishly difficult game. But if you have some kinds of objects in your game which get destroyed and respawned serveral times per second, then you might want to look into a pattern called "object pools". But that's a topic for a different question.

I am looking forward to playing your game.

added 112 characters in body
Source Link
Philipp
  • 123.2k
  • 28
  • 264
  • 345

As ThePumkinMelon suggested, create a prefab for your player game object.

Then remove the player object from your scene, have your player spawner reference the player prefab and also reference the current player game object in the scene:

public class PlayerSpawner : MonoBehaviour {

    public GameObject playerPrefab;
    private GameObject currentPlayer;

Then in the Update-method of the PlayerSpawner, do this:

    if (currentPlayer == null) {
         currentPlayer = Instantiate(playerPrefab, transform.position, transform.rotation);
    }

This will check if its player is in the scene, and when it is not it will spawn a new player at the position and with the rotation of the spawner (that means you need to place the PlayerSpawner game object at the location where you want the player to spawn).

There are two cases where currentPlayer == null will be true.

Now your Dead coroutine just needs to Destroy the player game object and the PlayerSpawner will instantiate a new one.


However, keep in mind that if you excessively destroy and instantiate a lot of game objects, you might start to notice notable performance degregation due to memory fragmentation and garbage collection. In this case this shouldn't matter, because the player will die every few seconds at most, even if you create a hellishly difficult game. But if you have some kinds of objects in your game which get destroyed and respawned serveral times per second, then you might want to look into a pattern called "object pools". But that's a topic for a different question.

I am looking forward to playing your game.

As ThePumkinMelon suggested, create a prefab for your player game object.

Then remove the player object from your scene, have your player spawner reference the player prefab and also reference the current player game object in the scene:

public class PlayerSpawner : MonoBehaviour {

    public GameObject playerPrefab;
    private GameObject currentPlayer;

Then in the Update-method of the PlayerSpawner, do this:

    if (currentPlayer == null) {
         currentPlayer = Instantiate(playerPrefab, transform.position, transform.rotation);
    }

This will check if its player is in the scene, and when it is not it will spawn a new player at the position and with the rotation of the spawner.

There are two cases where currentPlayer == null will be true.

Now your Dead coroutine just needs to Destroy the player game object and the PlayerSpawner will instantiate a new one.


However, keep in mind that if you excessively destroy and instantiate a lot of game objects, you might start to notice notable performance degregation due to memory fragmentation and garbage collection. In this case this shouldn't matter, because the player will die every few seconds at most, even if you create a hellishly difficult game. But if you have some kinds of objects in your game which get destroyed and respawned serveral times per second, then you might want to look into a pattern called "object pools". But that's a topic for a different question.

I am looking forward to playing your game.

As ThePumkinMelon suggested, create a prefab for your player game object.

Then remove the player object from your scene, have your player spawner reference the player prefab and also reference the current player game object in the scene:

public class PlayerSpawner : MonoBehaviour {

    public GameObject playerPrefab;
    private GameObject currentPlayer;

Then in the Update-method of the PlayerSpawner, do this:

    if (currentPlayer == null) {
         currentPlayer = Instantiate(playerPrefab, transform.position, transform.rotation);
    }

This will check if its player is in the scene, and when it is not it will spawn a new player at the position and with the rotation of the spawner (that means you need to place the PlayerSpawner game object at the location where you want the player to spawn).

There are two cases where currentPlayer == null will be true.

Now your Dead coroutine just needs to Destroy the player game object and the PlayerSpawner will instantiate a new one.


However, keep in mind that if you excessively destroy and instantiate a lot of game objects, you might start to notice notable performance degregation due to memory fragmentation and garbage collection. In this case this shouldn't matter, because the player will die every few seconds at most, even if you create a hellishly difficult game. But if you have some kinds of objects in your game which get destroyed and respawned serveral times per second, then you might want to look into a pattern called "object pools". But that's a topic for a different question.

I am looking forward to playing your game.

added 504 characters in body
Source Link
Philipp
  • 123.2k
  • 28
  • 264
  • 345

As ThePumkinMelon suggested, create a prefab for your player game object.

Then remove the player object from your scene, have your player spawner reference the player prefab and also reference the current player game object in the scene:

public class PlayerSpawner : MonoBehaviour {

    public GameObject playerPrefab;
    private GameObject currentPlayer;

Then in the Update-method of the PlayerSpawner, do this:

    if (currentPlayer == null) {
         currentPlayer = Instantiate(playerPrefab, transform.position, transform.rotation);
    }

This will check if its player is in the scene, and when it is not it will spawn a new player at the position and with the rotation of the spawner.

There are two cases where currentPlayer == null will be true.

Now your Dead coroutine just needs to Destroy the player game object and the PlayerSpawner will instantiate a new one.


However, keep in mind that if you excessively destroy and instantiate a lot of game objects, you might start to notice notable performance degregation due to memory fragmentation and garbage collection. In this case this shouldn't matter, because the player will die every few seconds at most, even if you create a hellishly difficult game. But if you have some kinds of objects in your game which get destroyed and respawned serveral times per second, then you might want to look into a pattern called "object pools". But that's a topic for a different question.

I am looking forward to playing your game.

As ThePumkinMelon suggested, create a prefab for your player game object.

Then remove the player object from your scene, have your player spawner reference the player prefab and also reference the current player game object in the scene:

public class PlayerSpawner : MonoBehaviour {

    public GameObject playerPrefab;
    private GameObject currentPlayer;

Then in the Update-method of the PlayerSpawner, do this:

    if (currentPlayer == null) {
         currentPlayer = Instantiate(playerPrefab, transform.position, transform.rotation)
    }

This will check if its player is in the scene, and when it is not it will spawn a new player at the position and with the rotation of the spawner.

There are two cases where currentPlayer == null will be true.

Now your Dead coroutine just needs to Destroy the player game object and the PlayerSpawner will instantiate a new one.

As ThePumkinMelon suggested, create a prefab for your player game object.

Then remove the player object from your scene, have your player spawner reference the player prefab and also reference the current player game object in the scene:

public class PlayerSpawner : MonoBehaviour {

    public GameObject playerPrefab;
    private GameObject currentPlayer;

Then in the Update-method of the PlayerSpawner, do this:

    if (currentPlayer == null) {
         currentPlayer = Instantiate(playerPrefab, transform.position, transform.rotation);
    }

This will check if its player is in the scene, and when it is not it will spawn a new player at the position and with the rotation of the spawner.

There are two cases where currentPlayer == null will be true.

Now your Dead coroutine just needs to Destroy the player game object and the PlayerSpawner will instantiate a new one.


However, keep in mind that if you excessively destroy and instantiate a lot of game objects, you might start to notice notable performance degregation due to memory fragmentation and garbage collection. In this case this shouldn't matter, because the player will die every few seconds at most, even if you create a hellishly difficult game. But if you have some kinds of objects in your game which get destroyed and respawned serveral times per second, then you might want to look into a pattern called "object pools". But that's a topic for a different question.

I am looking forward to playing your game.

Source Link
Philipp
  • 123.2k
  • 28
  • 264
  • 345
Loading