It looks like you're new here. If you want to get involved, click one of these buttons!
I'm following along with Brackey's "How to make a 2D game " series, and am now up to his wave spawner videos. Everything has been working fine so far, but now I'm running into an issue where I sometimes get two players spawning. I think it's happening if I am killed by two enemies simultaneously, so the _RespawnPlayer() method is being called twice.
The players both move around and shoot together, but can fall off the platforms separately. It's not exactly what I had intended so I'd love to hear if anyone has a fix for this!
Answers
Here's my GameMaster script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GameMaster : MonoBehaviour
{
public static GameMaster gm;
public CameraShake camShake; //reference to the camera shake
public float spawnCamShakeAmount = 0.05f;
public float spawnCamShakeDuration = 0.1f;
void Awake()
{
if (gm == null)
{
gm = this; //so we have a static variable called "gm" and we set it equal to this instance of the game master class if it's not already set
}
}
void Start()
{
camShake = GameMaster.gm.GetComponent<CameraShake>();
if (camShake == null)
{
Debug.LogError("No CameraShake script found on GM object.");
}
}
//variable containing reference to player Prefab
public GameObject playerPrefab;
//variable containing reference to spawn point
public Transform spawnPoint;
//create an integer containing seconds to delay before respawning
public float spawnDelay = 2f;
//variable containing reference to particle spawn prefab
public GameObject spawnPrefab; //don't need to use Transform if we're not going to access the Transform component (such as Transform.position), which we're not in this case
//underscore to signify that this is a LOCAL method, not a STATIC method
public IEnumerator _RespawnPlayer() //IEnumerator is a "coroutine" which allows the function to be delayed before being called (in the "yield return new" line below
{
//play the respawn audio clip which has been dragged onto the _GM
GetComponent<AudioSource>().Play();
yield return new WaitForSeconds(spawnDelay); //waits for the given number of seconds (determined by the spawnDelay variable) before respawning
Instantiate(playerPrefab, spawnPoint.position, spawnPoint.rotation); //instantiate player
camShake.Shake(spawnCamShakeAmount, spawnCamShakeDuration);
GameObject clone = Instantiate(spawnPrefab, spawnPoint.position, spawnPoint.rotation);
Destroy(clone, 3f); //this destroys the instantiated particles (which we've called "clone", which is standard practice for a briefly instantiated game object) after 3s
}
public static void KillPlayer(Player player) //function which accesses the Player class (which we made in the Player script), and we're calling the player "player"
{
Destroy(player.gameObject);
gm.StartCoroutine(gm._RespawnPlayer()); //calls the RespawnPlayer method defined above
}
public static void KillEnemy(Enemy enemy)
{
gm._KillEnemy(enemy); //call the _KillEnemy method we created below
}
public void _KillEnemy(Enemy _enemy) //underscore to signify that this is a LOCAL method, not a STATIC method
{
//initiate enemy death particles, at the enemy position, with no additional rotation
GameObject _clone = Instantiate(_enemy.deathParticles, _enemy.transform.position, Quaternion.identity) as GameObject;
//destroy the enemy death particles after 5 seconds
Destroy(_clone, 5f);
camShake.Shake(_enemy.enemyCamShakeAmount, _enemy.enemyCamShakeDuration);
Destroy(_enemy.gameObject);
}
}