Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

2D Platformer Game-Brackeys Computer Science 1 Question

jvernonjvernon Member
edited February 25 in Brackeys' Tutorials

I have 2 students that are working on the WaveSpawner extra videos. When the Player dies, it will do the countdown and respawn with no issues. But, sometimes it respawns with one instance of the Player but other times it respawns with mulitple instances of the Player. The particles do not work if the Player is duplicated after the respawn.


Both players have no coding errors, I have checked all their code . Any suggestions on what possibly is going wrong in their Game?

Answers

  • dommypdommyp Member

    I'm getting the same issue. It seems to happen when two enemies collide with me simultaneously to kill me, which makes me think that the _RespawnPlayer() method is being called twice. Would love to hear if anyone has a fix for this.

  • dommypdommyp Member

    Here's my GameMaster code:



    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);

      }

    }

Sign In or Register to comment.