Forum begins after the advertisement:


[Part 7] My Respawn button isnt working

Home Forums Video Game Tutorial Series Creating a Metroidvania in Unity [Part 7] My Respawn button isnt working

Viewing 15 posts - 1 through 15 (of 22 total)
  • Author
    Posts
  • #13611
    Anuk Thotawatta
    Participant

    when playercharacter dies and when i press the respawn button nothing happens(the button doesn’t even look like its clicking). And the button has been set to interactable.

    My “bench” code:

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    
    public class Coffin : MonoBehaviour
    {
        public bool interacted;
        // Start is called before the first frame update
        void Start()
        {
            
        }
    
        // Update is called once per frame
        void Update()
        {
            
        }
        private void OnTriggerStay2D(Collider2D _collision) {
            if(_collision.CompareTag("Player") && Input.GetButtonDown("Interact")){
                interacted = true;
            }
        }
    }

    my UIManager code:

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    
    public class Coffin : MonoBehaviour
    {
        public bool interacted;
        // Start is called before the first frame update
        void Start()
        {
            
        }
    
        // Update is called once per frame
        void Update()
        {
            
        }
        private void OnTriggerStay2D(Collider2D _collision) {
            if(_collision.CompareTag("Player") && Input.GetButtonDown("Interact")){
                interacted = true;
            }
        }
    }

    My gamemanager code:

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    
    public class GameManager : MonoBehaviour
    {
        public string transitionedFromScene;
        public Vector2 platformingRespawnPoint;
        public Vector2 respawnPoint;
        [SerializeField] Coffin coffin;
    
        public static GameManager Instance { get; private set; }
        private void Awake()
        {
            if(Instance != null && Instance != this)
            {
                Destroy(gameObject);
            }
            else
            {
                Instance = this;
            }
            DontDestroyOnLoad(gameObject);
            coffin = FindObjectOfType();
        }
    
        public void RespawnPlayer(){
            Debug.Log("Respawned");
            if(coffin != null){
                if(coffin.interacted){
                    respawnPoint = coffin.transform.position;
                }
                else{
                    respawnPoint = platformingRespawnPoint;
                }
            }
            else{
                respawnPoint = platformingRespawnPoint;
            }
            
            PlayerController.Instance.transform.position = respawnPoint;
    
            StartCoroutine(UIManager.Instance.DeactivateDeathScreen());
            PlayerController.Instance.Respawned();
        }
    }
    #13612
    Anuk Thotawatta
    Participant

    Update: put a event system on the canvas with stand alone input module and now, when i click button the game pauses and gives me the error “Coroutine couldn’t be started because the the game object ‘Game Manager’ is inactive!”

    #13614
    Terence
    Keymaster

    Hi Anuk, for the respawn button, have a read of this post and see if it helps fix your issue. It is related to the save issue with benches noted in this topic: https://blog.terresquall.com/community/topic/part-7-scenefader-nullreferenceexception-and-savedata-endofstreamexception/

    Update: put a event system on the canvas with stand alone input module and now, when i click button the game pauses and gives me the error “Coroutine couldn’t be started because the the game object ‘Game Manager’ is inactive!”

    For this issue, you will need to check your Hierarchy and see if the Game Manager object is inactive. If it is, what in your code is turning it inactive when the game is running?

    #13630
    Anuk Thotawatta
    Participant

    ok, now respawning kind of works for the rooms that dont have a bench. which means i respawn at the start point of the scene i died instead of the bench on a separate scene.

    also after respawning, why are my heartfills invisible? only after i get hit once my heart containers become visible again.

    #13633
    Terence
    Keymaster

    also after respawning, why are my heartfills invisible? only after i get hit once my heart containers become visible again.

    Check your heartContainers to see if you assigned the parent GameObject to one of the items.

    #13643
    Anuk Thotawatta
    Participant

    i went further into the save system video and now my character respawns at the bench if he dies in another scene, but doesnt respawn(respawn button unresponsive) if he dies in the scene where the bench is.

    another problem is that my cave_1 map is visible after starting the game as intended but after i interact with a bench in cave_4, instead of updating the scenes i came across, the map becomes invisible

    here’s the code for bench/coffin:

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.SceneManagement;
    
    public class Coffin : MonoBehaviour
    {
        public bool interacted;
        // Start is called before the first frame update
        void Start()
        {
            
        }
    
        // Update is called once per frame
        void Update()
        {
            
        }
        private void OnTriggerStay2D(Collider2D _collision) {
            if(_collision.CompareTag("Player") && Input.GetButtonDown("Interact")){
                interacted = true;
                SaveData.Instance.coffinSceneName = SceneManager.GetActiveScene().name;
                SaveData.Instance.coffinpos = new Vector2(gameObject.transform.position.x,gameObject.transform.position.y);
                SaveData.Instance.SaveCoffin();
            }
        }
        private void OnTriggerExit2D(Collider2D _collision) {
            if(_collision.CompareTag("Player")){
                interacted = false;
            }
        }
    }

    Map Manager cade:

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.UI;
    
    public class MapManager : MonoBehaviour
    {
        // Start is called before the first frame update
    
        [SerializeField] GameObject[] maps;
        Coffin coffin;
        
        private void OnEnable() {
            coffin = FindObjectOfType<Coffin>();
            if(coffin != null){
                if(coffin.interacted){
                    UpdateMap();
                }
            }
        }
    
        void UpdateMap(){
            var savedScenes = SaveData.Instance.sceneNames;
            for(int i=0;i<maps.Length;i++){
                if(savedScenes.Contains("Cave_"+i+1)){
                    maps[i].SetActive(true);
                }
                else{
                    maps[i].SetActive(false);
                }
            }
        }
    }
    
    #13646
    Terence
    Keymaster

    i went further into the save system video and now my character respawns at the bench if he dies in another scene, but doesnt respawn(respawn button unresponsive) if he dies in the scene where the bench is.

    When you mouse over the button, does it get highlighted?

    another problem is that my cave_1 map is visible after starting the game as intended but after i interact with a bench in cave_4, instead of updating the scenes i came across, the map becomes invisible

    When the map becomes invisible, do they still exist in the Hierarchy?

    #13650
    Anuk Thotawatta
    Participant

    no, if i die in the room with the bench the respawn button doesn’t get highlighted

    When the map becomes invisible, they exist in the Hierarchy. but they are turned off

    #13651
    Terence
    Keymaster

    no, if i die in the room with the bench the respawn button doesn’t get highlighted

    The respawn button may be blocked by another element in your scene. Can you try moving the button up so it becomes the first element under its parent?

    When the map becomes invisible, they exist in the Hierarchy. but they are turned off

    For this, can you check whether the scenes you have for the maps match the names of the GameObjects in your Map Manager component?

    #13654
    Anuk Thotawatta
    Participant

    View post on imgur.com

    the names of the scenes are correct and are in correct order

    #13655
    Terence
    Keymaster

    the names of the scenes are correct and are in correct order

    Move the Death Screen above the Scene Fader and see if it fixes the Respawn button not responding.

    #13669
    Anuk Thotawatta
    Participant

    I tried and that doesn’t seem to be the problem. when I move death screen above scene fader and play the scene fader sits on top of the death screen and I cant click the button. the respawn button works if I die in another scene. I cant interact with it only if I die in the scene with the bench in it.

    #13670
    #13674
    Terence
    Keymaster

    Can you show me your Hierarchy when the Respawn doesn’t work? Record something like the above.

    #13694
    Anuk Thotawatta
    Participant

    The Hierarchy’s dont look any different when i die in each scene.

    also in the video my heartfills are invisible after reaspawn. how do i fix that

Viewing 15 posts - 1 through 15 (of 22 total)
  • You must be logged in to reply to this topic.

Go to Login Page →


Advertisement below: