Forum begins after the advertisement:


[Part 6] Small question about optimization

Home Forums Video Game Tutorial Series Creating a Rogue-like Shoot-em Up in Unity [Part 6] Small question about optimization

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #17498
    Paulius Križinauskas
    Level 4
    Participant
    Helpful?
    Up
    0
    ::

    At 28:00 in the video once the code to destroySingleton gets added, when the scene changes from menu to game, the DontDestroyOnLoad tab doesn’t go away?

    My two files (which I think are the important ones for this):

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    
    public class CharacterSelector : MonoBehaviour
    {
        public static CharacterSelector instance;
        public CharacterScriptableObject characterData;
    
        void Awake()
        {
            if(instance == null)
            {
                instance = this;
                DontDestroyOnLoad(gameObject);
            }
            else
            {
                Debug.LogWarning("EXTRA" + this + " DELETED");
                Destroy(gameObject);
            }
        }
    
        public static CharacterScriptableObject GetData()
        {
            return instance.characterData;
        }
    
        public void SelectCharacter(CharacterScriptableObject character)
        {
            characterData = character;
        }
    
        public void DestroySingleton()
        {
            instance = null;
            Destroy(gameObject);
        }
    }
    
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    
    public class PlayerStats : MonoBehaviour
    {
        CharacterScriptableObject characterData;
    
        //current stats
        [HideInInspector]
        public float currentHealth;
        [HideInInspector]
        public float currentRecovery;
        [HideInInspector]
        public float currentMoveSpeed;
        [HideInInspector]
        public float currentMight;
        [HideInInspector]
        public float currentProjectileSpeed;
        [HideInInspector]
        public float currentMagnet;
    
        //experience
        [Header("Experience/Level")]
        public int experience = 0;
        public int level = 1;
        public int experienceCap;
    
        [System.Serializable] // kad galima inspectoriui daryt
        public class LevelRange
        {
            public int startLevel;
            public int endLevel;
            public int experienceCapIncrease;
        }
    
        [Header("I-Frames")]
        public float invincibilityDuration;
        float invincibilityTimer;
        bool isInvincible;
    
    
        public List<LevelRange> levelRanges;
    
        void Awake()
        {
            characterData = CharacterSelector.GetData();
            CharacterSelector.instance.DestroySingleton();
    
            currentHealth = characterData.MaxHealth;
            currentRecovery = characterData.Recovery;
            currentMoveSpeed = characterData.MoveSpeed;
            currentMight = characterData.Might;
            currentProjectileSpeed = characterData.ProjectileSpeed;
            currentMagnet = characterData.Magnet;
        }
    
        void Start()
        {
            experienceCap = levelRanges[0].experienceCapIncrease;
        }
    
        void Update()
        {
            if(invincibilityTimer > 0)
            {
                invincibilityTimer -= Time.deltaTime;
            }
            else if (isInvincible)
            {
                isInvincible = false;
            }
    
            Recovery();
        }
    
        public void IncreaseExperience(int amount)
        {
            experience += amount;
    
            LevelUpChecker();
        }
    
        void LevelUpChecker()
        {
            if (experience >= experienceCap)
            {
                level++;
                experience -= experienceCap;
    
                int experienceCapIncrease = 0;
                foreach (LevelRange range in levelRanges)
                {
                    if (level >= range.startLevel && level <= range.endLevel)
                    {
                        experienceCapIncrease = range.experienceCapIncrease;
                    }
                }
                experienceCap += experienceCapIncrease;
            }
        }
    
        public void TakeDamage(float dmg)
        {
            if(!isInvincible)
            {
                currentHealth -= dmg;
    
                invincibilityTimer = invincibilityDuration;
                isInvincible = true;
    
                if (currentHealth <= 0)
                {
                    Kill();
                }
            }
    
        }
    
        public void Kill()
        {
            Debug.Log("PLAYER IS DEAD");
        }
    
        public void RestoreHealth(float amount)
        {
            if(currentHealth < characterData.MaxHealth)
            {
                currentHealth += amount;
    
                if(currentHealth > characterData.MaxHealth)
                {
                    currentHealth = characterData.MaxHealth;
                }
            }
    
        }
    
        void Recovery()
        {
            if(currentHealth < characterData.MaxHealth)
            {
                currentHealth += currentRecovery * Time.deltaTime;
    
                if(currentHealth > characterData.MaxHealth)
                {
                    currentHealth = characterData.MaxHealth;
                }
            }
        }
    }
    

    Any help is appreciated!

    #17499
    Alp Apustaja
    Level 3
    AI Assistant
    (BETA)
    Helpful?
    Up
    0
    ::

    Based on the reference information provided, the solution is in the code you’ve already shown. In the PlayerStats script, you’re already calling CharacterSelector.instance.DestroySingleton(); in the Awake() method, which should remove the DontDestroyOnLoad object.

    If the object is not disappearing, double-check that:

    1. The CharacterSelector script is attached to a GameObject in the Menu scene
    2. The DestroySingleton() method is being called correctly
    3. There are no other references keeping the object alive

    If the issue persists, you may want to add a Debug.Log() in the DestroySingleton() method to verify it’s being called.

    #17500
    Paulius Križinauskas
    Level 4
    Participant
    Helpful?
    Up
    0
    ::

    Also, when I expand the DontDestroyOnLoad tab, in the menu scene it shows Character Selector and [Debug Updater], and after clicking on a character it only shows [Debug Updater].

    I am guessing this is the problem, but I don’t know where to find the piece of code that is adding this Debug Updater thing…

    #17501
    Alp Apustaja
    Level 3
    AI Assistant
    (BETA)
    Helpful?
    Up
    0
    ::

    It sounds like you’re dealing with a Unity project where the DontDestroyOnLoad function is being used to preserve certain objects between scene loads. The Debug Updater object might be a part of your code or a component that’s being instantiated but not properly managed.

    Here are some steps to help you track down the source of the Debug Updater:

    1. Search for “Debug Updater” in Your Code:

      • Use your IDE’s search function (usually Ctrl + F or Cmd + F) and search for Debug Updater. This should help you locate where it is being instantiated or referenced in your scripts.
    2. Check for DontDestroyOnLoad Calls:

      • Look through your scripts for any calls to DontDestroyOnLoad. This will help you identify which objects are being preserved between scenes and might lead you to the Debug Updater.
    3. Look at Scene Hierarchy:

      • When you run your game, take a close look at the Hierarchy in the Unity Editor. When you click on your character in the Character Selector, observe if the Debug Updater is a child of another object or if it’s instantiated as a sibling.
    4. Check Initialization Code:

      • Sometimes objects are created in the Start() or Awake() methods. Check your initialization code for any lines that may create or reference the Debug Updater.
    5. Debugging:

      • You can add debug logs in your scripts to track when and where the Debug Updater is being instantiated. For example, you can add Debug.Log("Debug Updater created"); in the constructor or instantiation method.
    6. Look for Singleton Patterns:

      • If you’re using a singleton pattern for your Debug Updater, it might be instantiated only once and is not being destroyed between scenes. Check the relevant script for such patterns.
    7. Review Prefabs:

      • If Debug Updater is a prefab, check the prefab settings and any scripts attached to it to see if it’s set to instantiate itself when a scene loads.

    If you still can’t find it after these steps, you can also consider temporarily disabling scripts or components one at a time to see if the Debug Updater disappears. This can help isolate which part of your codebase is responsible for it.

    Feel free to share any relevant code snippets or additional information if you’re still having trouble!

    #17508
    Paulius Križinauskas
    Level 4
    Participant
    Helpful?
    Up
    1
    ::

    For now I “Fixed” it by adding this under the destroySingleton call

        UnityEngine.Rendering.DebugManager.instance.enableRuntimeUI = false;

    I hope it doesn’t break anything, but for now it seems to work well, and I have marked it down in case something breaks in the future and this is the cause.

    has upvoted this post.
    #17509
    Terence
    Level 30
    Keymaster
    Helpful?
    Up
    1
    ::

    @pauliuskrizinauskas glad you managed to fix the issue. Feel free to make another post if you run into any other issue.

      1 anonymous person
    has upvoted this post.
Viewing 6 posts - 1 through 6 (of 6 total)
  • You must be logged in to reply to this topic.

Go to Login Page →


Advertisement below: