I wanted to give the player the choice of respawning with less money and health or reloading the last save file and here is how I did it if there is any better approach to do this let me know
public class Respawner : MonoBehaviour
{
[SerializeField] Transform respawnLocation;
[SerializeField] float fadeTime = 0.2f;
[SerializeField] float healPercetnage = 30f;
[SerializeField] float respawnCost = 50;
LazyValue<SavingWrapper> savingWrapper;
private void Awake()
{
savingWrapper = new LazyValue<SavingWrapper>(GetSavingWrapper);
}
private SavingWrapper GetSavingWrapper()
{
return FindObjectOfType<SavingWrapper>();
}
//with less health
public void Respawn()
{
StartCoroutine(RespawnRotuin());
}
//Reload last save
public void Restart()
{
savingWrapper.value.ContinueGame();
}
private IEnumerator RespawnRotuin()
{
yield return new WaitForSeconds(1);
Fader fader = FindObjectOfType<Fader>();
yield return fader.FadeOut(fadeTime);
GetComponent<NavMeshAgent>().Warp(respawnLocation.position);
Health health= GetComponent<Health>();
health.Heal(health.GetMaxHealthPoints() * healPercetnage / 100);
Purse purse= GetComponent<Purse>();
purse.UpdateBalance(-purse.GetBalance() * respawnCost / 100);
yield return fader.FadeIn(fadeTime);
}
}