- Home /
PlayerRespawn class wont Instantiate the player prefab
Specifically, when the player Die()'s, the public int lives is subtracted through live--. The part of player script is:
[SerializeField] PlayerRespawn playerRespawner;
private void OnTriggerEnter2D(Collider2D enemy)
{
DamageDealer damageDealer = enemy.gameObject.GetComponent<DamageDealer>();
if (!damageDealer) { return; }
ProcessHit(damageDealer);
}
private void ProcessHit(DamageDealer damageDealer)
{
health -= damageDealer.GetDamage();
damageDealer.Hit();
if (health <= 0)
{
Die();
}
}
private void Die()
{
Destroy(gameObject);
AudioSource.PlayClipAtPoint
(deathSound,
Camera.main.transform.position,
deathSoundVolume);
if (playerRespawner.lives > 0)
{
StartCoroutine(playerRespawner.RespawnPlayer());
}
playerRespawner.lives--;
}
Now, for the PlayerRespawn Class,
{
[SerializeField] SceneLoader sceneLoader;
[SerializeField] GameObject playerPrefab;
public int lives;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
if(lives <= 0)
{
StartCoroutine(GameOver());
}
}
public IEnumerator RespawnPlayer()
{
yield return new WaitForSeconds(2);
GameObject player = Instantiate(playerPrefab, transform.position, Quaternion.identity) as GameObject;
}
public IEnumerator GameOver()
{
yield return new WaitForSeconds(3);
sceneLoader.GetComponent<SceneLoader>().LoadNextScene();
}
}
I tried manually assigning the prefab/gameObjects to the serialized fields, but the Coroutine just wont start.
Answer by cemcakirca54 · Sep 28, 2019 at 10:07 PM
Die function is destroying your gameObject which has player script, so you can't start coroutine. Use destroy function after start coroutine and don't use Camera.main if you can. I don't know your player completely but you can also try respawn without using Instantiate. Set player transform to respawn transform. Edit: Don't use update in PlayerRespawn class, you can check with Die function.
private void Die()
{
AudioSource.PlayClipAtPoint
(deathSound,
Camera.main.transform.position,
deathSoundVolume);
playerRespawner.lives--;
if (playerRespawner.lives > 0)
StartCoroutine(playerRespawner.RespawnPlayer());
Destroy(gameObject);
}
PlayerRespawn Class
public IEnumerator RespawnPlayer()
{
yield return new WaitForSeconds(2);
if(lives <= 0)
{
StartCoroutine(GameOver());
yield break;
}
GameObject player = Instantiate(playerPrefab, transform.position, Quaternion.identity) as GameObject;
}
Or you can create function in your PlayerRespawn class for check player lives.
private void Die()
{
AudioSource.PlayClipAtPoint
(deathSound,
Camera.main.transform.position,
deathSoundVolume);
playerRespawner.Die();
Destroy(gameObject);
}
PlayerRespawn Class
[SerializeField] SceneLoader sceneLoader;
[SerializeField] GameObject playerPrefab;
[SerializeField] private int lives;
public void Die()
{
lives--;
if(lives == 0)
StartCoroutine(GameOver());
else
StartCoroutine(RespawnPlayer());
}
IEnumerator RespawnPlayer()
{
yield return new WaitForSeconds(2);
GameObject player = Instantiate(playerPrefab, transform.position, Quaternion.identity)
as GameObject;
}
IEnumerator GameOver() {
yield return new WaitForSeconds(3);
sceneLoader.GetComponent().LoadNextScene();
}