- Home /
Intermittent bug occurring when re spawning
Guys I've got an intermittent re-spawning issue. If my character receives to kill shots within milliseconds of each other. Rather than re-spawning like normal on mobile devices it re-spawns my character twice. if anyone could point me in the right direction i'd appreciate it. using System.Collections; using System.Collections.Generic; using UnityEngine;
public class GameMaster : MonoBehaviour
{
public static GameMaster gm;
[SerializeField]
private int maxLives = 3;
private static int _remainingLives = 3;
public static int RemainingLives
{
get { return _remainingLives; }
}
void Awake()
{
if (gm == null)
{
gm = GameObject.FindGameObjectWithTag("GM").GetComponent<GameMaster>();
}
}
public Transform playerPrefab;
public Transform spawnPoint;
public float spawnDelay = 2;
public Transform spawnPrefab;
public string respawnCountdownSoundName = "RespawnCountdown";
public string spawnSoundName = "Spawn";
public string gameOverSoundName = "GameOver";
public CameraShake cameraShake;
[SerializeField]
private GameObject gameOverUI;
// Cache
private AudioManager audioManger;
//public HealthBar healthBar;
void Start()
{
if(cameraShake == null)
{
Debug.LogError("No camera shake");
}
_remainingLives = maxLives;
// Caching
audioManger = AudioManager.instance;
if (audioManger == null)
{
Debug.LogError("FREAK OUT! No Audio manager found in scene");
}
}
public void EndGame()
{
audioManger.PlaySound(gameOverSoundName);
Debug.Log("EndGame");
gameOverUI.SetActive(true);
}
public IEnumerator _RespawnPlayer ()
{
audioManger.PlaySound(respawnCountdownSoundName);
yield return new WaitForSeconds(spawnDelay);
audioManger.PlaySound(spawnSoundName);
Instantiate(playerPrefab, spawnPoint.position, spawnPoint.rotation);
GameObject clone = Instantiate(spawnPrefab.gameObject, spawnPoint.position, spawnPoint.rotation) as GameObject;
Destroy(clone, 4f);
}
public static void KillPlayer(Player player)
{
_remainingLives -= 1;
if(_remainingLives <= 0)
{
gm.EndGame();
}
else
{
gm.StartCoroutine(gm._RespawnPlayer());
}
Destroy(player.gameObject);
}
public static void KillEnemy (Enemy enemy)
{
gm._killEnemy(enemy);
}
public void _killEnemy(Enemy _enemy)
{
// Let's play some sound
audioManger.PlaySound(_enemy.deathSoundName);
// Add Particles
GameObject _clone = Instantiate(_enemy.deathParticles.gameObject, _enemy.transform.position, Quaternion.identity) as GameObject;
Destroy(_clone, 5f);
// Camera Shake
//cameraShake.Shake(_enemy.shakeAmt, _enemy.shakeLength);
Destroy(_enemy.gameObject);
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Player : MonoBehaviour
{
[System.Serializable]
public class PlayerStats
{
/*public int Health = 100;*/
public int maxHealth;
public HealthBar healthBar;
private int _curHealth;
public int curHealth
{
get { return _curHealth; }
set { _curHealth = Mathf.Clamp(value, 0, maxHealth); }
}
public void Init()
{
curHealth = maxHealth;
healthBar.SetMaxHealth(maxHealth);
}
}
public PlayerStats stats = new PlayerStats();
public int fallBoundary = -20;
public string deathSoundName = "DeathVoice";
public string damageSoundName = "Grunt";
private AudioManager audioManager;
[SerializeField]
private StatusIndicator statusIndicator;
void Start()
{
stats.Init();
if(statusIndicator == null)
{
Debug.LogError("Error");
}
else
{
statusIndicator.SetHealth(stats.curHealth, stats.maxHealth);
}
audioManager = AudioManager.instance;
if (audioManager == null)
{
Debug.LogError("No audio Manager in scene");
}
}
void Update()
{
if(transform.position.y <= fallBoundary)
DamagePlayer(999999);
}
public void DamagePlayer (int damage)
{
stats.curHealth -= damage;
stats.healthBar.SetHealth(stats.curHealth);
if (stats.curHealth <= 0)
{
// Play death sound
audioManager.PlaySound(deathSoundName);
// Kill player
GameMaster.KillPlayer(this);
} else
{
// Play damage sound
audioManager.PlaySound(damageSoundName);
}
statusIndicator.SetHealth(stats.curHealth, stats.maxHealth);
}
}
Answer by Llama_w_2Ls · Jul 28, 2020 at 03:51 PM
Cant you just put in the Awake method: if (gm == null) { gm = this} else {Destroy(gameObject)}
This would mean that only one instance of your character exists at one time, meaning that you cant respawn twice.
Answer by unity_ek98vnTRplGj8Q · Jul 28, 2020 at 04:08 PM
Just keep a flag that keeps track of whether or not you have called the "KillPlayer" method yet and return out of the method if you have.