Question by
Jogile · May 12, 2018 at 11:21 PM ·
platformerrespawnlives
cant stand in front of the platform after respawning
I am doing 2D platformer and did the script so the player is able to stand in front of the platform, but then I added the respawning thing and 3 lives to the player and after I fall off into the water my player can not stand in front of the platform as it used to
Comment
Good day.
We are not magicians... if you expect help, give us all the possible information. Post code, screenshots, say what you find, what your tried...
Bye.
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Game$$anonymous$$aster : $$anonymous$$onoBehaviour {
public static Game$$anonymous$$aster gm;
[SerializeField]
private int maxLives = 3;
private static int _remainingLives;
public static int RemainingLives
{
get { return _remainingLives; }
}
void Awake ()
{
if (gm == null)
{
gm = GameObject.FindGameObjectWithTag ("G$$anonymous$$").GetComponent<Game$$anonymous$$aster>();
}
}
public string respawnCountdownSoundName = "RespawnCountdown";
public string spawnSoundName = "Spawn";
public string gameOverSoundName = "GameOver";
//cache
private Audio$$anonymous$$anager audio$$anonymous$$anager;
void Start ()
{
_remainingLives = maxLives;
//caching
audio$$anonymous$$anager = Audio$$anonymous$$anager.instance;
if (audio$$anonymous$$anager == null)
{
Debug.LogError("FREA$$anonymous$$ OUT! No Audio$$anonymous$$anger found in the scene.");
}
}
public Transform playerPrefab;
public Transform spawnPoint;
public int spawnDelay = 2;
[SerializeField]
private GameObject gameOverUI;
public void EndGame ()
{
audio$$anonymous$$anager.PlaySound(gameOverSoundName);
Debug.Log("GA$$anonymous$$E OVER");
gameOverUI.SetActive(true);
}
public IEnumerator RespawnPlayer()
{
audio$$anonymous$$anager.PlaySound(respawnCountdownSoundName);
yield return new WaitForSeconds(spawnDelay);
audio$$anonymous$$anager.PlaySound(spawnSoundName);
Instantiate (playerPrefab, spawnPoint.position, spawnPoint.rotation);
}
public static void $$anonymous$$illPlayer(Player player)
{
Destroy(player.gameObject);
_remainingLives -= 1;
if (_remainingLives <= 0)
{
gm.EndGame();
} else
{
gm.StartCoroutine(gm.RespawnPlayer());
}
}
}
Answer by Jogile · May 13, 2018 at 04:05 PM
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;
public static int RemainingLives
{
get { return _remainingLives; }
}
void Awake ()
{
if (gm == null)
{
gm = GameObject.FindGameObjectWithTag ("GM").GetComponent<GameMaster>();
}
}
public string respawnCountdownSoundName = "RespawnCountdown";
public string spawnSoundName = "Spawn";
public string gameOverSoundName = "GameOver";
//cache
private AudioManager audioManager;
void Start ()
{
_remainingLives = maxLives;
//caching
audioManager = AudioManager.instance;
if (audioManager == null)
{
Debug.LogError("FREAK OUT! No AudioManger found in the scene.");
}
}
public Transform playerPrefab;
public Transform spawnPoint;
public int spawnDelay = 2;
[SerializeField]
private GameObject gameOverUI;
public void EndGame ()
{
audioManager.PlaySound(gameOverSoundName);
Debug.Log("GAME OVER");
gameOverUI.SetActive(true);
}
public IEnumerator RespawnPlayer()
{
audioManager.PlaySound(respawnCountdownSoundName);
yield return new WaitForSeconds(spawnDelay);
audioManager.PlaySound(spawnSoundName);
Instantiate (playerPrefab, spawnPoint.position, spawnPoint.rotation);
}
public static void KillPlayer(Player player)
{
Destroy(player.gameObject);
_remainingLives -= 1;
if (_remainingLives <= 0)
{
gm.EndGame();
} else
{
gm.StartCoroutine(gm.RespawnPlayer());
}
}
}