When the player dies the game is to restart in 5 seconds.
But it keeps on restarting 5 seconds after the game starts. I had the restart code in a separate script but integrated it into the Player Death script just to make it easier.
The 1st place I put the invoke code was after every if-statement and the game didnt restart at all.
then tried it at the end of the OnTriggerEnter and that is when the game restarts after 5 seconds.
the code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class PlayerDeath : MonoBehaviour
{
public GameObject objectToDestroy;
public GameObject playerExplosionEffect;
public float restartDelay = 5f;
private AudioSource audioSource;
void OnTriggerEnter(Collider other)
{
if (other.tag == "Enemy")
{
Instantiate(playerExplosionEffect, transform.position, transform.rotation);
Destroy(gameObject);
vibratePhone();
Invoke("Restart", restartDelay); //NB: first place for invoke
Debug.Log("Player touches enemy: Player Death script");
}
if (other.tag == "Obsidian")
{
Instantiate(playerExplosionEffect, transform.position, transform.rotation);
Destroy(gameObject);
vibratePhone();
Invoke("Restart", restartDelay); //NB: first place for invoke
Debug.Log("Player touches Obsidian: Player Death script");
}
if (other.tag == "EnemyBolt")
{
Instantiate(playerExplosionEffect, transform.position, transform.rotation);
Destroy(gameObject);
vibratePhone();
Invoke("Restart", restartDelay); //NB: first place for invoke
Debug.Log("Player hit: Player Death script");
}
Invoke("Restart", restartDelay); //NB: second place for invoke
//FindObjectOfType<NewGameManager>().EndGame(); //NB: This was to direct the code to the original restart script
}
public void vibratePhone()
{
Handheld.Vibrate();
Debug.Log("Vibration: Player Death script");
}
public void Restart()
{
SceneManager.LoadScene(SceneManager.GetActiveScene().name);
Debug.Log("Game Over: Game Manager script");
}
}
Does anyone have any suggestions?