- Home /
How do I get this simple respawn system working in?
I'm working on a 3D game in Unity where you die if you fall off the platforms. The aim is to respawn after you fall off a platform. At first it worked, but after I made a completely new movement system (with a character controller), a new player and a new camera it no longer works.
I want to make the player respawn after falling off a platform, I used a large cube under the platforms that serves as a death trigger. After the player comes into contact with it, he must randomly appear on one of the nine platforms. Looks like the random-spawnpoint-picker still works. The only problem is that the player does not respawn. In very very rare cases the player does respawn. How can I get this working consistent?
I would really appreciate help! I really tried my best to explain everything as clearly as possible :)
Detailed information:
- I placed 9 empty game objects above the platforms with a "SpawnPoint" tag 
- The selected spawnpoint will be a random picked number (0-8) and is activated at start and when the player comes into contact with the death trigger 
- The player can't move anymore when extra lives < 0 
- The spawnpoints will be destroyed if the player reaches 0 extra hearts 
- The water has no colliders or anything 
 
The scene - platforms with one of the respawn points selected (might be useful for context)  
 
Respawn script attached to the player:
 public class PlayerRespawn : MonoBehaviour
 {
 
     // Spawning
     [SerializeField] private string tagName;
     [SerializeField] private GameObject[] spawnPoints;
     [SerializeField] private GameObject selectedSpawnpoint;
 
     // Sound Effects
     public AudioSource source;
     public AudioClip clip;
 
     void Start()
     {
         int rand = Random.Range(0, 8);
         selectedSpawnpoint = spawnPoints [rand];
     }
 
     void Update()
     {
         if(spawnPoints == null)
         {
             spawnPoints = GameObject.FindGameObjectsWithTag(tag);
         }
     }
 
     void OnTriggerEnter(Collider col)
     {
         if(col.transform.tag == "DeathTrigger")
         {
             this.transform.position = selectedSpawnpoint.transform.position;
 
             source.PlayOneShot(clip);
 
             Debug.Log("AU!");
         }
 
         int rand = Random.Range(0, 8);
         selectedSpawnpoint = spawnPoints [rand];
     }
 }
Player lives script (not sure if this script has anything to do with the problem):
 public class PlayerLives : MonoBehaviour
  {
      public int extraPlayerHearts = 3;
      
      void OnTriggerEnter(Collider other)
      {
          extraPlayerHearts = extraPlayerHearts - 1;
  
          // The spawnpoints will be destroyed if the player reaches 0 extra hearts
          if(extraPlayerHearts <= 0)
          {
              GameObject[] foundObjects = GameObject.FindGameObjectsWithTag("SpawnPoint");
  
              foreach (GameObject go in foundObjects)
              {
                  Destroy(go);
              }
          }
  
          if(extraPlayerHearts < 0)
          {
              Debug.Log("You Died!");
              GetComponent<PlayerController>().enabled = false;
          }
      }
  }
The scripts in the inspector: 
Answer by Kessoe · Apr 16 at 07:55 PM
After a week of struggling, I have a working script! It's a mix of two replies to this forum post.
This code works for me:
 public class NewPlayerRespawn : MonoBehaviour
 {
     // Spawning
     [SerializeField] private GameObject[] spawnPoints;
     [SerializeField] private GameObject selectedSpawnpoint;
     private Transform transformSpawnpoint;
     public GameObject playerPrefab;
     private GameObject newPlayer;
 
     // Sound Effects
     public AudioSource source;
     public AudioClip clip;
 
     void Start()
     {
         int rand = Random.Range(0, 8);
         selectedSpawnpoint = spawnPoints [rand];
 
         transformSpawnpoint = selectedSpawnpoint.transform;
     }
 
     void Update()
     {
         if(this.transform.position.y <= 2f)
         {
             Respawn();
             int rand = Random.Range(0, 8);
             selectedSpawnpoint = spawnPoints [rand];
         }
     }
 
     void Respawn()
     {
         Destroy(gameObject);
         newPlayer = Instantiate(playerPrefab, transformSpawnpoint.position, transformSpawnpoint.rotation);
     }
 }
Answer by divinereignoflords · Apr 10 at 06:06 PM
  public string player_prefab; # this is your character, which has the camera and all attached to it
  public Transform spawn_point; # the empty/object where you want respawn to occur
 
 
 if(extraPlayerHearts < 0)
           {
               Debug.Log("You Died!");
               GetComponent<PlayerController>().enabled = false;
               Spawn();
           }
 
 public void Spawn()
         {
             Player = Instantiate(player_prefab, spawn_point.position, spawn_point.rotation);
       
 
   }
 
try something like this
You have to change string to GameObject... I think OP made a mistake.
yes its GameObject sorry, let me know if that works or helps
It still doesn't work, I get errors with "public void Spawn()" (I tried deleting public) and "Player" (Player is no thing (I tried changing Player into player_prefab)). Even after I made some adjustments it still doesn't work.
Answer by yahlimem · Apr 15 at 04:13 PM
Maybe just check for the Y position of the player If(player.transform.position <= the Y you want) Respawn();
Your answer
 
 
              koobas.hobune.stream
koobas.hobune.stream 
                       
               
 
			 
                