Forum begins after the advertisement:
[part 6] spikes freeze game
Home › Forums › Video Game Tutorial Series › Creating a Metroidvania in Unity › [part 6] spikes freeze game
- This topic has 6 replies, 3 voices, and was last updated 11 months ago by Aka.
-
AuthorPosts
-
December 11, 2023 at 8:13 am #12568::
not sure why – no compiler or any errors occuring – when i touchs spikes, my game will freeze. ive set debug logs in every stage of the IEnumerator and it seens to not work when the scene fader occurs (it works during scene transitions)
ive set a respawner in my game and all script stuff is properreference for my spike script – debug log will print – 1, 2, 3, 4, respawn, takes damage
using System.Collections; using System.Collections.Generic; using UnityEngine; public class Spikes : MonoBehaviour { // Start is called before the first frame update void Start() { } // Update is called once per frame void Update() { } private void OnTriggerEnter2D(Collider2D _other) { if (_other.CompareTag("Player")) { StartCoroutine(RespawnPoint()); Debug.Log("respawn"); } } IEnumerator RespawnPoint() { PlayerController.Instance.pState.cutscene = true; Debug.Log("1"); PlayerController.Instance.pState.invincible = true; Debug.Log("2"); PlayerController.Instance.rb.velocity = Vector2.zero; Debug.Log("3"); Time.timeScale = 0; StartCoroutine(UIManager.Instance.sceneFader.Fade(SceneFader.FadeDirection.In)); Debug.Log("4"); PlayerController.Instance.TakeDamage(1); Debug.Log("Takes damage"); yield return new WaitForSeconds(1); Debug.Log("5"); PlayerController.Instance.transform.position = GameManager.Instance.platformingRespawnPoint; Debug.Log("6"); StartCoroutine(UIManager.Instance.sceneFader.Fade(SceneFader.FadeDirection.Out)); Debug.Log("7"); yield return new WaitForSeconds(UIManager.Instance.sceneFader.fadeTime); Debug.Log("8"); PlayerController.Instance.pState.cutscene = false; PlayerController.Instance.pState.invincible = false; Debug.Log("9"); Time.timeScale = 1; } }
using System; using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using UnityEngine.SceneManagement; public class SceneFader : MonoBehaviour { [SerializeField] public float fadeTime; private Image fadeOutUIImage; public enum FadeDirection { In, Out } private void Awake() { fadeOutUIImage = GetComponent<Image>(); } public IEnumerator Fade(FadeDirection _fadeDirection) { float _alpha = _fadeDirection == FadeDirection.Out ? 1 : 0; float _fadeEndValue = _fadeDirection == FadeDirection.Out ? 0 : 1; if (_fadeDirection == FadeDirection.Out) { while (_alpha >= _fadeEndValue) { SetColorImage(ref _alpha, _fadeDirection); yield return null; } fadeOutUIImage.enabled = false; } else { fadeOutUIImage.enabled = true; while (_alpha <= _fadeEndValue) { SetColorImage(ref _alpha, _fadeDirection); yield return null; } } } public IEnumerator FadeAndLoadScene(FadeDirection _fadeDirection, string _sceneToLoad) { fadeOutUIImage.enabled = true; yield return Fade(_fadeDirection); SceneManager.LoadScene(_sceneToLoad); } void SetColorImage(ref float _alpha, FadeDirection _fadeDirection) { fadeOutUIImage.color = new Color(fadeOutUIImage.color.r, fadeOutUIImage.color.g, fadeOutUIImage.color.b, _alpha); _alpha += Time.deltaTime * (1 / fadeTime) * (_fadeDirection == FadeDirection.Out ? -1 : 1); } }
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using UnityEngine.SceneManagement; public class SceneTransition : MonoBehaviour { [SerializeField] private string transitionTo; //Represents the scene to transition to [SerializeField] private Transform startPoint; //Defines the player's entry point in the scene [SerializeField] private Vector2 exitDirection; //Specifies the direction for the player's exit [SerializeField] private float exitTime; //Determines the time it takes for the player to exit the scene transition // Start is called before the first frame update private void Start() { if (GameManager.Instance.transitionedFromScene == transitionTo) { PlayerController.Instance.transform.position = startPoint.position; StartCoroutine(PlayerController.Instance.WalkIntoNewScene(exitDirection, exitTime)); } StartCoroutine(UIManager.Instance.sceneFader.Fade(SceneFader.FadeDirection.Out)); } private void OnTriggerEnter2D(Collider2D _other) { if (_other.CompareTag("Player")) { GameManager.Instance.transitionedFromScene = SceneManager.GetActiveScene().name; SceneManager.LoadScene(transitionTo); PlayerController.Instance.pState.cutscene = true; StartCoroutine(UIManager.Instance.sceneFader.FadeAndLoadScene(SceneFader.FadeDirection.In, transitionTo)); } } }
using System.Collections; using System.Collections.Generic; using UnityEngine; public class GameManager : MonoBehaviour { public string transitionedFromScene; public Vector2 platformingRespawnPoint; public static GameManager Instance { get; private set; } private void Awake() { if(Instance != null && Instance != this) { Destroy(gameObject); } else { Instance = this; } DontDestroyOnLoad(gameObject); } }
using System.Collections; using System.Collections.Generic; using UnityEngine; public class PlatformRespawnPoint : MonoBehaviour { // Start is called before the first frame update void Start() { } // Update is called once per frame void Update() { } private void OnTriggerEnter2D(Collider2D _other) { if (_other.CompareTag("Player")) { GameManager.Instance.platformingRespawnPoint = transform.position; } } }
December 11, 2023 at 5:14 pm #12570::Hi Niyam,
If a game freezes when you run code, usually its because you have a loop that does not terminate. Try commenting your while loops out in
SceneFader
and see if the freeze still happens.public IEnumerator Fade(FadeDirection _fadeDirection) { float _alpha = _fadeDirection == FadeDirection.Out ? 1 : 0; float _fadeEndValue = _fadeDirection == FadeDirection.Out ? 0 : 1; if (_fadeDirection == FadeDirection.Out) { while (_alpha >= _fadeEndValue) { SetColorImage(ref _alpha, _fadeDirection); yield return null; } fadeOutUIImage.enabled = false; } else { fadeOutUIImage.enabled = true; while (_alpha <= _fadeEndValue) { SetColorImage(ref _alpha, _fadeDirection); yield return null; } } }
If this fixes the issue, look into your while loop’s condition and try to find out what is causing the condition to never be met.
December 11, 2023 at 6:13 pm #12573December 11, 2023 at 6:14 pm #12575December 12, 2023 at 3:20 pm #12587::Try disabling the
StartCoroutine()
on your Spike script and see if it fixes the crash. The other possibility is that it might beRespawnPoint()
that is causing the crash.private void OnTriggerEnter2D(Collider2D _other) { if (_other.CompareTag("Player")) {
StartCoroutine(RespawnPoint());Debug.Log("respawn"); } }December 13, 2023 at 1:54 am #12623::O ye alr fixed this
The computing respawn ppint was the cause
This was in ur video ….
In the cooroutine u had 2 yield return waitforsecondsThe yield return part of these broke the coroutine
I removed these and added a
Yield return waitforseconds(0);
At the end of my script and it works now perfectlyDecember 21, 2023 at 11:12 pm #12727 -
AuthorPosts
- You must be logged in to reply to this topic.
Advertisement below: