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
- This topic has 4 replies, 2 voices, and was last updated 11 months, 4 weeks ago by Niyam Shah.
-
AuthorPosts
-
December 17, 2023 at 1:04 am #12691::
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; } } }
December 17, 2023 at 7:40 pm #12694::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 becauseOnTriggerStay2D()
doesn’t always fire on the same frames asUpdate()
, so reading input onOnTriggerStay2D()
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();</marK> } } 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-createdinteractable
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.December 17, 2023 at 7:55 pm #12696::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?
December 17, 2023 at 8:02 pm #12697::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); } } }
December 17, 2023 at 8:24 pm #12698::thanks dude i got it to work – i just copied and pasted the update map code u pasted above and its working again
-
AuthorPosts
- You must be logged in to reply to this topic.
Advertisement below: