Click here to Skip to main content
15,881,651 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i am trying to code a death script that restarts the scene when you die, this is my script:

C#
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class PlayerDeath : MonoBehaviour
{
    [SerializeField]
    string Player2Tag;

    void Update()
    {

    }

    private void OnCollisionEnter(Collision collision)
    {
        if (collision.collider.tag == Player2Tag)
            SceneManager.LoadScene(SceneManager.GetActiveScene().name);
    }
}


i have set the string Player2Tag as Player2 and for some reason the restart activates when a player with the tag Player (without the number 2) it still activates.
can some one help me?

What I have tried:

i have tried this changing the tag, didn't work
untaged objects don't activate the restart,
this secondey script is for the other tag i have tried:
C#
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class RestartManager : MonoBehaviour
{
    [SerializeField]
    KeyCode keyRestart;
    [SerializeField]
    string PlayerTag;

    void Update()
    {
        if (Input.GetKey(keyRestart))
            SceneManager.LoadScene(SceneManager.GetActiveScene().name);
    }

    private void OnCollisionEnter(Collision collision)
    {
        if (collision.collider.tag == PlayerTag)
            SceneManager.LoadScene(SceneManager.GetActiveScene().name);
    }
}
Posted
Updated 11-May-20 1:28am
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900