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

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

    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 proper

    reference 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;
            }
        }
    }
    #12570
    Terence
    Level 30
    Keymaster
    Helpful?
    Up
    0
    ::

    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.

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

    i tried it and no luck – still broken and freezes at the same palce?

    #12575
    Niyam Shah
    Level 8
    Participant
    #12587
    Terence
    Level 30
    Keymaster
    Helpful?
    Up
    0
    ::

    Try disabling the StartCoroutine() on your Spike script and see if it fixes the crash. The other possibility is that it might be RespawnPoint() that is causing the crash.

        private void OnTriggerEnter2D(Collider2D _other)
        {
            if (_other.CompareTag("Player"))
            {
                StartCoroutine(RespawnPoint());
                Debug.Log("respawn");
            }
        }
    #12623
    Niyam Shah
    Level 8
    Participant
    Helpful?
    Up
    0
    ::

    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 waitforseconds

    The 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 perfectly

    #12727
    Aka
    Level 4
    Participant
    Helpful?
    Up
    0
    ::

    Remplace WaitForSeconds by WaitForSecondsRealtime in the coroutine.

    You can’t have WaitForSeconds with Timescale to 0.

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

Go to Login Page →


Advertisement below: