- Home /
How to respawn player game object after game over
Hi,
I am trying to respawn the player after pressing the continue button and hiding the game over screen. Here is the code so far.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class UnitManager : MonoBehaviour
{
public PlayerController ball;
public float respawnDelay = 5;
public GameObject currentCheckpoint;
public GameStatus gs;
private CountdownTimer ct;
public GameObject destroyParticle;
public GameObject respawnParticle;
private float gravityStore;
private HealthManager hm;
public LivesSystem ls;
public CameraController cam;
public Reset res;
public GameObject gameOver;
void Start()
{
ball = FindObjectOfType<PlayerController>();
gs = FindObjectOfType<GameStatus>();
ct = FindObjectOfType<CountdownTimer>();
hm = FindObjectOfType<HealthManager>();
ls = FindObjectOfType<LivesSystem>();
cam = FindObjectOfType<CameraController>();
res = FindObjectOfType<Reset>();
}
public void RespawnPlayer()
{
hm.DestroyPlayer();
if (gs.lives >= 2)
{
StartCoroutine("RespawnPlayerCo");
}
else
{
Over();
}
}
public void Over()
{
StartCoroutine("OverCo");
}
public IEnumerator RespawnPlayerCo()
{
Instantiate(destroyParticle, ball.transform.position, ball.transform.rotation);
ball.enabled = false;
ball.GetComponent<Renderer>().enabled = false;
cam.isFollowing = false;
//gravityStore = ball.GetComponent<Rigidbody2D>().gravityScale;
//ball.GetComponent<Rigidbody2D>().gravityScale = 0f;
//ball.GetComponent<Rigidbody2D>().velocity = Vector2.zero;
Debug.Log("Player Respawned: " + transform.position);
yield return new WaitForSeconds(respawnDelay);
//ball.GetComponent<Rigidbody2D>().gravityScale = gravityStore;
ball.transform.position = currentCheckpoint.transform.position;
ball.enabled = true;
ball.GetComponent<Renderer>().enabled = true;
hm.Revive();
cam.isFollowing = true;
Instantiate(respawnParticle, currentCheckpoint.transform.position, currentCheckpoint.transform.rotation);
}
public IEnumerator OverCo()
{
Instantiate(destroyParticle, ball.transform.position, ball.transform.rotation);
cam.isFollowing = false;
ball.GetComponent<Renderer>().enabled = false;
//gravityStore = ball.GetComponent<Rigidbody2D>().gravityScale;
//ball.GetComponent<Rigidbody2D>().gravityScale = 0f;
//ball.GetComponent<Rigidbody2D>().velocity = Vector2.zero;
yield return new WaitForSecondsRealtime(3);
gameOver.gameObject.SetActive(true);
}
public void DoOver()
{
StartCoroutine("Again");
}
public IEnumerator Again()
{
gameOver.gameObject.SetActive(false);
Debug.Log("Player Respawned: " + transform.position);
yield return new WaitForSeconds(1);
//ball.GetComponent<Rigidbody2D>().gravityScale = gravityStore;
ball.transform.position = currentCheckpoint.transform.position;
ball.enabled = true;
ball.GetComponent<Renderer>().enabled = true;
hm.Revive();
cam.isFollowing = true;
Instantiate(respawnParticle, currentCheckpoint.transform.position, currentCheckpoint.transform.rotation);
}
public void StartOver()
{
SceneManager.LoadScene("Intro");
}
}
Any assistance would be appreciated.
Sincerely,
burchland2
Answer by Unrighteouss · Aug 31, 2021 at 03:29 PM
Make the player a prefab, then Instantiate the prefab. I can see that you've already used Instantiate a bunch, so you probably won't have trouble doing that.
Answer by burchland2 · Aug 31, 2021 at 06:13 PM
I tried:
Instantiate(ball); Instantiate(player); I assigned the player to both the ball and player variables. (I already made the player a prefab. Instantiate(ball, ball.transform.position, ball.transform.rotation); Instantiate(player, ball.transform.position, ball.transform.rotation); The latter was a small mistake on my part. But the same thing occurred with no player popping up. What else should I try? Is there something that I'm missing? Thank you.
Sincerely,
burchland2
Make sure that you set the spawn position and rotation in the Instantiate method, not the object's position/rotation.
That being said, it's difficult to understand what the issue is. Does the player not appear? Does Instantiate not work? Add debug lines to see what's happening, and why the behavior isn't what you expect: https://docs.unity3d.com/ScriptReference/Debug.Log.html
Sorry for not specifying, but the player does not appear. The source of the problem was that, in the hierarchy, the clone was not selected, but it was grayed out.
So you're saying the player starts inactive? If that's the case, you just need to set the player to active:
[SerializeField] GameObject player;
[SerializeField] Transform spawnPoint;
void Start()
{
GameObject newPlayer = Instantiate(player, spawnPoint.position, Quaternion.identity);
newPlayer.SetActive(true);
}
Your answer
Follow this Question
Related Questions
Why doesnt my respawn script work? 2 Answers
Respawn back at the old position. 1 Answer
Unity 2D Text Mesh disappear 0 Answers
Flip 2D Character with Keys without moving 2 Answers
Problem with Scrip for sprites 1 Answer