Bishara Khuri
Bishara Khuri

Reputation: 11

Beginner having Problems with Restarting Scenes when Dying in Unity

so I’m trying to create a respawn system as a beginner, and everything seems to be working the first time but the second time it seems to be unbehaving. If anyone knows how to help me, I would appreciate it

LevelControl:

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

public class LevelControl : MonoBehaviour
{

    public int index;
    public string levelName;
    public GameObject GameOverPanel;
    public static LevelControl instance;

    private void Awake()
    {
        if (instance == null)
        {
            DontDestroyOnLoad(gameObject);
            instance = GetComponent<LevelControl>();
        }
    }

    void OnTriggerEnter2D(Collider2D other)
    {
        if (other.CompareTag("Player"))
        {
            //Loading level with build index
            SceneManager.LoadScene(index);

            //Loading level with scene name
            SceneManager.LoadScene(levelName);

            //Restart level
            //SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
        }
    }
    public void LoadLevel1()
    {
        SceneManager.LoadScene("Game");
    }
    public GameObject GetGameOverScreen()
    {
        return GameOverPanel;
    }
}

PlayerMovement:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerMovement : MonoBehaviour
{

    public CharacterController2D controller;

    public float runSpeed = 40f;

    float horizontalMove = 0f;
    bool jump = false;
    bool crouch = false;

    // Update is called once per frame
    void Update()
    {

        horizontalMove = Input.GetAxisRaw("Horizontal") * runSpeed;

        if (Input.GetButtonDown("Jump"))
        {
            jump = true;
        }

        if (Input.GetButtonDown("Crouch"))
        {
            crouch = true;
        }
        else if (Input.GetButtonUp("Crouch"))
        {
            crouch = false;
        }

    }

    void FixedUpdate()
    {
        // Move our character
        controller.Move(horizontalMove * Time.fixedDeltaTime, crouch, jump);
        jump = false;
        //FindObjectOfType<GameManager>().EndGame();
    }
    void OnCollisionEnter2D(Collision2D collision)
    {
        if (collision.gameObject.tag == "Enemy")
        {
            //Destroy(FindObjectOfType<CharacterController2D>().gameObject);
            GameObject.Find("Player").SetActive(false);
            LevelControl.instance.GetGameOverScreen().SetActive(true);
        }
    }
}

Error In Unity Video: https://i.sstatic.net/VFBKE.jpg

If your wondering what I'm trying to do, well, when the 2 colliders of the Player and Enemy collide, I want a restart button to pop up, and the Character to get destroyed, and after that restart the level as is.

Upvotes: 0

Views: 297

Answers (2)

user21200345
user21200345

Reputation:

Ok, Normally it would be easier just to do this:

 if (collision.gameObject.tag == "Enemy")
    {
        //Destroy(FindObjectOfType<CharacterController2D>().gameObject);
        gameObject.SetActive(false);
        LevelControl.instance.GetGameOverScreen().SetActive(true);

But this will NOT work if you want to attach the script to any other gameobjects for any reason. If you are then first make a game object variable containing the player, like this:

public GameObject Player = GameObject.Find("Player");

Then say

Player.SetActive(false);

This creates a player gameobject which you can access by calling the variable.

Upvotes: 0

derHugo
derHugo

Reputation: 90724

You didn't provide much but I try to work with what we have.

In LevelController you have

private void Awake()
{
    if (instance == null)
    {
        DontDestroyOnLoad(gameObject);
        instance = GetComponent<LevelControl>();
    }
}

First of all just use

instance = this;

;)


Then you are doing

LevelControl.instance.GetGameOverScreenn().SetActive(true);

I don't see your setup but probably GetGameOverScreenn might not exist anymore after the Scene reload while the instance still does due to the DontDestroyOnLoad.

Actually, why even use a Singleton here? If you reload the entire scene anyway you could just setup the references once via the Inspector and wouldn't have to worry about them later after scene changes...


Also

GameObject.Find("Player").SetActive(false);

seems odd .. isn't your PlayerController attached to the Player object anyway? You could just use

gameObject.SetActive(false);

Upvotes: 3

Related Questions