Forum begins after the advertisement:


[part 7] map stopped updating

Home Forums Video Game Tutorial Series Creating a Metroidvania in Unity [part 7] map stopped updating

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #12691
    Niyam Shah
    Level 8
    Participant
    Helpful?
    Up
    0
    ::

    my maps update worked whenever i saved – i got to the end of the video, my saving works but now my map wont update – if i turn on my map bits in the nspector then they are permanently visible – nothing wrong in the inspector as it worked previously – cant see an error in my code??

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    
    public class MapManager : MonoBehaviour
    {
        [SerializeField] GameObject[] map;
    
        SavePoint savePoint;
    
        private void OnEnable()
        {
            savePoint = FindObjectOfType<SavePoint>();
    
            if(savePoint != null)
            {
                if(savePoint.interacted)
                {
                    UpdateMap();
                }
            }
        }
        // Start is called before the first frame update
        void Start()
        {
            
        }
    
        // Update is called once per frame
        void Update()
        {
            
        }
    
        void UpdateMap()
        {
            var savedScene = SaveData.Instance.sceneNames;
    
            for (int i = 0; i < map.Length; i++)
            {
                if(savedScene.Contains("Cave_" + (i + 1)))
                {
                    map[i].SetActive(true);
                }
                else
                {
                    map[i].SetActive(false);
                }
            }
        }
    }
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.SceneManagement;
    
    public class SavePoint : 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.savePointSceneName = SceneManager.GetActiveScene().name;
                SaveData.Instance.savePointPos = new Vector2(gameObject.transform.position.x, gameObject.transform.position.y);
                SaveData.Instance.SaveSavePoint();
                SaveData.Instance.SavePlayerData();
            }
        }
    
        private void OnTriggerExit2D(Collider2D _collision)
        {
            if (_collision.CompareTag("Player"))
            {
                interacted = false;
            }
        }
    }
    #12694
    Terence
    Level 30
    Keymaster
    Helpful?
    Up
    0
    ::

    There doesn’t seem to be any issue here.

    One potential issue I’ve spotted (a mistake from the series) is that you shouldn’t try to read input in OnTriggerStay2D(). The reason for that is because OnTriggerStay2D() doesn’t always fire on the same frames as Update(), so reading input on OnTriggerStay2D() may lead to dropped inputs. If you want to understand more about this, the post here will be perfect: https://blog.terresquall.com/2020/08/unity-rigidbodys-interpolate-property/

    Try doing something like this on SavePoint instead.

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

    I would replace interacted with the newly-created interactable variable, but I’m not sure if it is used in a future part, so I don’t want to change it in case it affects codes from future parts.

    #12696
    Niyam Shah
    Level 8
    Participant
    Helpful?
    Up
    0
    ::

    yep so i tried this and still no luck

    just to clarify – this was working until i finshed off the video

    the screen will go slightly darker when i hit the map button (due to the map handler image)n- you can see in the inspector that the maphandler and maps both become active when i click “m” but the children of maps wont become active after saving or when moving scenes and saving – if i manually turn them on in the inspector then they are visisble — its an issue with the map updates?

    #12697
    Terence
    Level 30
    Keymaster
    Helpful?
    Up
    0
    ::

    You can add Debug.Log() here to check if they are running or not.

        void UpdateMap()
        {
            var savedScene = SaveData.Instance.sceneNames;
    
            for (int i = 0; i < map.Length; i++)
            {
                if(savedScene.Contains("Cave_" + (i + 1)))
                {
                    Debug.Log(savedScene + " set to active");
                    map[i].SetActive(true);
                }
                else
                {
                    Debug.Log(savedScene + " set to inactive");
                    map[i].SetActive(false);
                }
            }
        }
    #12698
    Niyam Shah
    Level 8
    Participant
    Helpful?
    Up
    0
    ::

    thanks dude i got it to work – i just copied and pasted the update map code u pasted above and its working again

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

Go to Login Page →


Advertisement below: