- Home /
Respawn item if player dies
So I have a checkpoint script that I've been culminating over the past months to the point that you activate a checkpoint using a matchstick.
If you have no matchsticks then you can't activate the checkpoint. when activating a checkpoint it will save your current matches after saving into a temp variable. If you then collect 2 more matchsticks but die before you can activate a new checkpoint then you get reset back to temp variable total.
For example steps:
Start with 3 Matchsticks
Activate Checkpoint & lose 1 matchstick from activating = 2 matchsticks
Temp variable saves your 2 matchsticks
Find 2 more matchsticks and make your total 4 matchsticks
Player dies
Player respawn with 2 matchsticks
Now the problem I'm trying to fix is when the player respawns with those 2 matchsticks the level respawns the matchstick box objects that player interacts with.
So e.g. the player found 2 matchboxes containing a matchstick in each of them.
Upon collecting the matchsticks it destroys the 2 matchboxes.
If the player activates a checkpoint then those matchboxes will remain destroyed. If the player dies before activating a checkpoint then those matchboxes that were collected will respawn once again in the same locations they were previously.
This is my level manager script:
public class LevelManager : MonoBehaviour {
public GameObject currentCheckpoint;
private FirstPersonController player;
public Checkpoint match;
void Start ()
{ player = FindObjectOfType<FirstPersonController>();
match = FindObjectOfType<Checkpoint>();
}
public void RespawnPlayer()
{ Debug.Log("Respawned Player");
player.transform.position = currentCheckpoint.transform.position;
match.respawn = true;
} }
This is my checkpoint script (Only part that matters is the if (respawn == true)).
void Update()
{
if (Input.GetKeyDown(KeyCode.F) && inZone && levelManager.currentCheckpoint != gameObject)
{
// StopCoroutine("Opacity");
// triggered = false;
if (_currentMatches >= 1)
{
levelManager.currentCheckpoint = gameObject;
gameObject.GetComponentInChildren<ParticleSystem>().Play();
gameObject.GetComponentInChildren<Light>().enabled = true;
_currentMatches--;
tempMatches = _currentMatches;
matches_text.text = "X " + _currentMatches;
// StartCoroutine("Opacity");
// _HUD_Text.text = "Checkpoint Activated";
}
else if (_currentMatches <= 0)
{ _currentMatches = 0; }
}
/* if (inZone && levelManager.currentCheckpoint != gameObject)
{
_HUD_Text.text = "Checkpoint Discovered \nPress F to activate";
StartCoroutine("Opacity");
}*/
if (!inZone && levelManager.currentCheckpoint != gameObject)
{
gameObject.GetComponentInChildren<ParticleSystem>().Stop();
gameObject.GetComponentInChildren<Light>().enabled = false;
}
if (respawn == true)
{
Debug.Log("Just respawned");
_currentMatches = tempMatches;
Debug.Log("Number of matches is:" + _currentMatches);
matches_text.text = "X " + _currentMatches;
respawn = false;
}