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
- This topic has 3 replies, 3 voices, and was last updated 3 weeks ago by
Terence.
-
AuthorPosts
-
March 11, 2025 at 5:24 am #17498::
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!
March 11, 2025 at 5:31 am #17499::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:
- The CharacterSelector script is attached to a GameObject in the Menu scene
- The DestroySingleton() method is being called correctly
- 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.
March 11, 2025 at 5:36 am #17500::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…
March 11, 2025 at 5:40 am #17501::It sounds like you’re dealing with a Unity project where the
DontDestroyOnLoad
function is being used to preserve certain objects between scene loads. TheDebug 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
:-
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.
- Use your IDE’s search function (usually Ctrl + F or Cmd + F) and search for
-
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 theDebug Updater
.
- Look through your scripts for any calls to
-
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.
- 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
-
Check Initialization Code:
- Sometimes objects are created in the
Start()
orAwake()
methods. Check your initialization code for any lines that may create or reference theDebug Updater
.
- Sometimes objects are created in the
-
Debugging:
- You can add debug logs in your scripts to track when and where the
Debug Updater
is being instantiated. For example, you can addDebug.Log("Debug Updater created");
in the constructor or instantiation method.
- You can add debug logs in your scripts to track when and where the
-
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.
- If you’re using a singleton pattern for your
-
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
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!
March 11, 2025 at 12:09 pm #17508::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. March 11, 2025 at 4:27 pm #17509::@pauliuskrizinauskas glad you managed to fix the issue. Feel free to make another post if you run into any other issue.
- 1 anonymous person
-
AuthorPosts
- You must be logged in to reply to this topic.
Advertisement below: