Forum begins after the advertisement:
[part 10.5] saving the boss causes an error
Home › Forums › Video Game Tutorial Series › Creating a Metroidvania in Unity › [part 10.5] saving the boss causes an error
- This topic has 10 replies, 2 voices, and was last updated 4 weeks ago by Chloe Lim.
-
AuthorPosts
-
October 19, 2024 at 1:01 am #16111::
i added the save feature and keep getting this error along with the debug logs of “boss deosnt exist” so my boss never spawns
here is my spawn point script:
using System.Collections; using System.Collections.Generic; using UnityEngine; public class spawnBoss : MonoBehaviour { public static spawnBoss Instance; [SerializeField] Transform spawnPoint; [SerializeField] GameObject boss; [SerializeField] Vector2 exitDirection; [SerializeField] GameObject bossRubble; bool callOnce; BoxCollider2D col; private void Awake() { if(WoodenBot.Instance != null) { Destroy(WoodenBot.Instance); callOnce = false; col.isTrigger = true; } if (GameManager.Instance.woodenBotDefeated == true) { callOnce = true; } if(Instance != null && Instance != this) { Destroy(gameObject); } else { Instance = this; } } // Start is called before the first frame update void Start() { col = GetComponent<BoxCollider2D>(); } // Update is called once per frame void Update() { } void OnTriggerEnter2D(Collider2D _other) { if (_other.CompareTag("Player") && !callOnce && !GameManager.Instance.woodenBotDefeated) { StartCoroutine(walkIntoRoom()); callOnce = true; } } IEnumerator walkIntoRoom() { StartCoroutine(PlayerController.Instance.WalkIntoNewScene(exitDirection, 1)); PlayerController.Instance.pState.cutscene = true; yield return new WaitForSeconds(5f); col.isTrigger = false; Destroy(bossRubble); Instantiate(boss, spawnPoint.position, Quaternion.identity); } public void isNotTrigger() { col.isTrigger = true; } }
the part of code that causes the error is :
if (GameManager.Instance.woodenBotDefeated == true) { callOnce = true; }
when i remove this, the game can actually run but my boss will never spawn and the “boss doesnt exist” debug logswill play twice
my savedata script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
using UnityEngine.SceneManagement;[System.Serializable]
public struct SaveData
{
public static SaveData Instance;//maps
public HashSet<string> sceneNames;//savePoints
public string savePointSceneName;
public Vector2 savePointPos;//player
public int playerHealth;
public float playerMana;public Vector2 playerPosition;
public string lastScene;public bool playerUnlockedWallJump;
public bool playerUnlockedDash;
public bool playerUnlockedDoubleJump;
public bool playerunlockedUpSpellExplosion;
public bool playerunlockedHeal;public int playerHeartsShards;
public int playerManaOrbs;
public int playerOrbShards;
public float playerOrb0fill, playerOrb1fill, playerOrb2fill;//Bossses
public bool woodenBotDefeated;
public void Initialize()
{
if (!File.Exists(Application.persistentDataPath + “/save.savePoint.data”))
{
BinaryWriter writer = new BinaryWriter(File.Create(Application.persistentDataPath + “/save.savePoint.data”));
}if (!File.Exists(Application.persistentDataPath + “/save.player.data”))
{
BinaryWriter writer = new BinaryWriter(File.Create(Application.persistentDataPath + “/save.player.data”));
}if (!File.Exists(Application.persistentDataPath + “/save.boss.data”))
{
BinaryWriter writer = new BinaryWriter(File.Create(Application.persistentDataPath + “/save.boss.data”));
}if (sceneNames == null)
{
sceneNames = new HashSet<string>();
}
}public void SaveSavePoint()
{
using (BinaryWriter writer = new BinaryWriter(File.OpenWrite(Application.persistentDataPath + “/save.savePoint.data”)))
{
writer.Write(savePointSceneName);
writer.Write(savePointPos.x);
writer.Write(savePointPos.y);
}
}public void LoadSavePoint()
{
if (!File.Exists(Application.persistentDataPath + “/save.savePoint.data”))
{
using (BinaryReader reader = new BinaryReader(File.OpenRead(Application.persistentDataPath + “/save.savePoint.data”)))
{
savePointSceneName = reader.ReadString();
savePointPos.x = reader.ReadSingle();
savePointPos.y = reader.ReadSingle();
}}
}public void SavePlayerData()
{
using (BinaryWriter writer = new BinaryWriter(File.OpenWrite(Application.persistentDataPath + “/save.player.data”)))
{
playerHealth = PlayerController.Instance.Health;
writer.Write(playerHealth);playerHeartsShards = PlayerController.Instance.heartShards;
writer.Write(playerHeartsShards);playerOrbShards = PlayerController.Instance.orbShards;
writer.Write(playerOrbShards);playerManaOrbs = PlayerController.Instance.manaOrbs;
writer.Write(playerManaOrbs);playerOrb0fill = PlayerController.Instance.manaOrbsHandler.orbFills[0].fillAmount;
writer.Write(playerOrb0fill);playerOrb1fill = PlayerController.Instance.manaOrbsHandler.orbFills[1].fillAmount;
writer.Write(playerOrb1fill);playerOrb2fill = PlayerController.Instance.manaOrbsHandler.orbFills[2].fillAmount;
writer.Write(playerOrb2fill);playerMana = PlayerController.Instance.Mana;
writer.Write(playerMana);playerUnlockedWallJump = PlayerController.Instance.unlockWallJump;
writer.Write(playerUnlockedWallJump);playerUnlockedDash = PlayerController.Instance.unlockDash;
writer.Write(playerUnlockedDash);playerUnlockedDoubleJump = PlayerController.Instance.unlockDoubleJump;
writer.Write(playerUnlockedDoubleJump);playerunlockedUpSpellExplosion = PlayerController.Instance.unlockUpSpellExplosion;
writer.Write(playerunlockedUpSpellExplosion);playerunlockedHeal = PlayerController.Instance.unlockHeal;
writer.Write(playerunlockedHeal);playerPosition = PlayerController.Instance.transform.position;
writer.Write(playerPosition.x);
writer.Write(playerPosition.y);lastScene = SceneManager.GetActiveScene().name;
writer.Write(lastScene);
}
}public void LoadPlayerData()
{
if (File.Exists(Application.persistentDataPath + “/save.player.data”))
{
using (BinaryReader reader = new BinaryReader(File.OpenRead(Application.persistentDataPath + “/save.player.data”)))
{
playerHealth = reader.ReadInt32();
playerHeartsShards = reader.ReadInt32();playerOrbShards = reader.ReadInt32();
playerManaOrbs = reader.ReadInt32();
playerOrb0fill = reader.ReadSingle();
playerOrb1fill = reader.ReadSingle();
playerOrb2fill = reader.ReadSingle();playerMana = reader.ReadSingle();
playerUnlockedWallJump = reader.ReadBoolean();
playerUnlockedDash = reader.ReadBoolean();
playerUnlockedDoubleJump = reader.ReadBoolean();
playerunlockedUpSpellExplosion = reader.ReadBoolean();
playerunlockedHeal = reader.ReadBoolean();playerPosition.x = reader.ReadSingle();
playerPosition.y = reader.ReadSingle();lastScene = reader.ReadString();
SceneManager.LoadScene(lastScene);
PlayerController.Instance.transform.position = playerPosition;
PlayerController.Instance.Health = playerHealth;
PlayerController.Instance.heartShards = playerHeartsShards;
PlayerController.Instance.orbShards = playerOrbShards;
PlayerController.Instance.Mana = playerMana;
PlayerController.Instance.manaOrbs = playerManaOrbs;
PlayerController.Instance.manaOrbsHandler.orbFills[0].fillAmount = playerOrb0fill;
PlayerController.Instance.manaOrbsHandler.orbFills[1].fillAmount = playerOrb1fill;
PlayerController.Instance.manaOrbsHandler.orbFills[2].fillAmount = playerOrb2fill;
PlayerController.Instance.unlockWallJump = playerUnlockedWallJump;
PlayerController.Instance.unlockDash = playerUnlockedDash;
PlayerController.Instance.unlockDoubleJump = playerUnlockedDoubleJump;
PlayerController.Instance.unlockUpSpellExplosion = playerunlockedUpSpellExplosion;
PlayerController.Instance.unlockHeal = playerunlockedHeal;
}
}
else
{
Debug.Log(“file doesnt exist”);
PlayerController.Instance.Health = PlayerController.Instance.maxHealth;
PlayerController.Instance.heartShards = 0;
PlayerController.Instance.orbShards = 0;
PlayerController.Instance.manaOrbs = 0;
PlayerController.Instance.Mana = 0.5f;
PlayerController.Instance.unlockWallJump = false;
PlayerController.Instance.unlockDash = false;
PlayerController.Instance.unlockDoubleJump = false;
PlayerController.Instance.unlockUpSpellExplosion = false;
PlayerController.Instance.unlockHeal = false;
}
}public void SaveBossData()
{
using (BinaryWriter writer = new BinaryWriter(File.OpenWrite(Application.persistentDataPath + “/save.boss.data”)))
{
woodenBotDefeated = GameManager.Instance.woodenBotDefeated;writer.Write(woodenBotDefeated);
}
}public void LoadBossData()
{
if (!File.Exists(Application.persistentDataPath + “/save.boss.data”))
{
using (BinaryReader reader = new BinaryReader(File.OpenRead(Application.persistentDataPath + “/save.boss.data”)))
{
woodenBotDefeated = reader.ReadBoolean();GameManager.Instance.woodenBotDefeated = woodenBotDefeated;
}}
else
{
Debug.Log(“boss doesnt exist”);
}
}}
`
`my gamemanager script:
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.SceneManagement; using UnityEngine.UI; using System.IO; public class GameManager : MonoBehaviour { public string transitionedFromScene; public Vector2 platformingRespawnPoints; public Vector2 respawnPoints; [SerializeField] SavePoint savePoint; public bool woodenBotDefeated = false; [SerializeField] private FadeUI pauseMenu; [SerializeField] private float fadeTime; public bool gameIsPaused; [SerializeField] AudioClip caveMusic; AudioSource audioSource; private void Start() { audioSource = GetComponent<AudioSource>(); audioSource.clip = caveMusic; StartCoroutine(LoopAudio()); } IEnumerator LoopAudio() { float length = audioSource.clip.length; while (true) { audioSource.Play(); yield return new WaitForSeconds(length); } } private void CaveMusic() { audioSource.PlayOneShot(caveMusic); } public static GameManager Instance { get; private set; } private void Awake() { SaveData.Instance.Initialize(); if(Instance != null && Instance != this) { Destroy(gameObject); } else { Instance = this; } SaveScene(); DontDestroyOnLoad(gameObject); savePoint = FindObjectOfType<SavePoint>(); SaveData.Instance.LoadBossData(); } public void SaveScene() { string currentSceneName = SceneManager.GetActiveScene().name; SaveData.Instance.sceneNames.Add(currentSceneName); } private void Update() { if (Input.GetKeyDown(KeyCode.P)) { SaveData.Instance.SavePlayerData(); } if (Input.GetKeyDown(KeyCode.Escape) && !gameIsPaused) { pauseMenu.fadeUIIn(fadeTime); Time.timeScale = 0; gameIsPaused = true; } } public void UnpauseGame() { Time.timeScale = 1; gameIsPaused = false; } public void RespawnPlayer() { SaveData.Instance.LoadSavePoint(); if(SaveData.Instance.savePointSceneName != null) { SceneManager.LoadScene(SaveData.Instance.savePointSceneName); } if(SaveData.Instance.savePointPos != null) { respawnPoints = SaveData.Instance.savePointPos; } PlayerController.Instance.transform.position = respawnPoints; StartCoroutine(UIManager.Instance.DeactiveDeathScreen()); PlayerController.Instance.Respawned(); } }
October 19, 2024 at 1:02 am #16112::savedata script didnt send as code here it is properly:
using System.Collections; using System.Collections.Generic; using UnityEngine; using System.IO; using UnityEngine.SceneManagement; [System.Serializable] public struct SaveData { public static SaveData Instance; //maps public HashSet<string> sceneNames; //savePoints public string savePointSceneName; public Vector2 savePointPos; //player public int playerHealth; public float playerMana; public Vector2 playerPosition; public string lastScene; public bool playerUnlockedWallJump; public bool playerUnlockedDash; public bool playerUnlockedDoubleJump; public bool playerunlockedUpSpellExplosion; public bool playerunlockedHeal; public int playerHeartsShards; public int playerManaOrbs; public int playerOrbShards; public float playerOrb0fill, playerOrb1fill, playerOrb2fill; //Bossses public bool woodenBotDefeated; public void Initialize() { if (!File.Exists(Application.persistentDataPath + "/save.savePoint.data")) { BinaryWriter writer = new BinaryWriter(File.Create(Application.persistentDataPath + "/save.savePoint.data")); } if (!File.Exists(Application.persistentDataPath + "/save.player.data")) { BinaryWriter writer = new BinaryWriter(File.Create(Application.persistentDataPath + "/save.player.data")); } if (!File.Exists(Application.persistentDataPath + "/save.boss.data")) { BinaryWriter writer = new BinaryWriter(File.Create(Application.persistentDataPath + "/save.boss.data")); } if (sceneNames == null) { sceneNames = new HashSet<string>(); } } public void SaveSavePoint() { using (BinaryWriter writer = new BinaryWriter(File.OpenWrite(Application.persistentDataPath + "/save.savePoint.data"))) { writer.Write(savePointSceneName); writer.Write(savePointPos.x); writer.Write(savePointPos.y); } } public void LoadSavePoint() { if (!File.Exists(Application.persistentDataPath + "/save.savePoint.data")) { using (BinaryReader reader = new BinaryReader(File.OpenRead(Application.persistentDataPath + "/save.savePoint.data"))) { savePointSceneName = reader.ReadString(); savePointPos.x = reader.ReadSingle(); savePointPos.y = reader.ReadSingle(); } } } public void SavePlayerData() { using (BinaryWriter writer = new BinaryWriter(File.OpenWrite(Application.persistentDataPath + "/save.player.data"))) { playerHealth = PlayerController.Instance.Health; writer.Write(playerHealth); playerHeartsShards = PlayerController.Instance.heartShards; writer.Write(playerHeartsShards); playerOrbShards = PlayerController.Instance.orbShards; writer.Write(playerOrbShards); playerManaOrbs = PlayerController.Instance.manaOrbs; writer.Write(playerManaOrbs); playerOrb0fill = PlayerController.Instance.manaOrbsHandler.orbFills[0].fillAmount; writer.Write(playerOrb0fill); playerOrb1fill = PlayerController.Instance.manaOrbsHandler.orbFills[1].fillAmount; writer.Write(playerOrb1fill); playerOrb2fill = PlayerController.Instance.manaOrbsHandler.orbFills[2].fillAmount; writer.Write(playerOrb2fill); playerMana = PlayerController.Instance.Mana; writer.Write(playerMana); playerUnlockedWallJump = PlayerController.Instance.unlockWallJump; writer.Write(playerUnlockedWallJump); playerUnlockedDash = PlayerController.Instance.unlockDash; writer.Write(playerUnlockedDash); playerUnlockedDoubleJump = PlayerController.Instance.unlockDoubleJump; writer.Write(playerUnlockedDoubleJump); playerunlockedUpSpellExplosion = PlayerController.Instance.unlockUpSpellExplosion; writer.Write(playerunlockedUpSpellExplosion); playerunlockedHeal = PlayerController.Instance.unlockHeal; writer.Write(playerunlockedHeal); playerPosition = PlayerController.Instance.transform.position; writer.Write(playerPosition.x); writer.Write(playerPosition.y); lastScene = SceneManager.GetActiveScene().name; writer.Write(lastScene); } } public void LoadPlayerData() { if (File.Exists(Application.persistentDataPath + "/save.player.data")) { using (BinaryReader reader = new BinaryReader(File.OpenRead(Application.persistentDataPath + "/save.player.data"))) { playerHealth = reader.ReadInt32(); playerHeartsShards = reader.ReadInt32(); playerOrbShards = reader.ReadInt32(); playerManaOrbs = reader.ReadInt32(); playerOrb0fill = reader.ReadSingle(); playerOrb1fill = reader.ReadSingle(); playerOrb2fill = reader.ReadSingle(); playerMana = reader.ReadSingle(); playerUnlockedWallJump = reader.ReadBoolean(); playerUnlockedDash = reader.ReadBoolean(); playerUnlockedDoubleJump = reader.ReadBoolean(); playerunlockedUpSpellExplosion = reader.ReadBoolean(); playerunlockedHeal = reader.ReadBoolean(); playerPosition.x = reader.ReadSingle(); playerPosition.y = reader.ReadSingle(); lastScene = reader.ReadString(); SceneManager.LoadScene(lastScene); PlayerController.Instance.transform.position = playerPosition; PlayerController.Instance.Health = playerHealth; PlayerController.Instance.heartShards = playerHeartsShards; PlayerController.Instance.orbShards = playerOrbShards; PlayerController.Instance.Mana = playerMana; PlayerController.Instance.manaOrbs = playerManaOrbs; PlayerController.Instance.manaOrbsHandler.orbFills[0].fillAmount = playerOrb0fill; PlayerController.Instance.manaOrbsHandler.orbFills[1].fillAmount = playerOrb1fill; PlayerController.Instance.manaOrbsHandler.orbFills[2].fillAmount = playerOrb2fill; PlayerController.Instance.unlockWallJump = playerUnlockedWallJump; PlayerController.Instance.unlockDash = playerUnlockedDash; PlayerController.Instance.unlockDoubleJump = playerUnlockedDoubleJump; PlayerController.Instance.unlockUpSpellExplosion = playerunlockedUpSpellExplosion; PlayerController.Instance.unlockHeal = playerunlockedHeal; } } else { Debug.Log("file doesnt exist"); PlayerController.Instance.Health = PlayerController.Instance.maxHealth; PlayerController.Instance.heartShards = 0; PlayerController.Instance.orbShards = 0; PlayerController.Instance.manaOrbs = 0; PlayerController.Instance.Mana = 0.5f; PlayerController.Instance.unlockWallJump = false; PlayerController.Instance.unlockDash = false; PlayerController.Instance.unlockDoubleJump = false; PlayerController.Instance.unlockUpSpellExplosion = false; PlayerController.Instance.unlockHeal = false; } } public void SaveBossData() { using (BinaryWriter writer = new BinaryWriter(File.OpenWrite(Application.persistentDataPath + "/save.boss.data"))) { woodenBotDefeated = GameManager.Instance.woodenBotDefeated; writer.Write(woodenBotDefeated); } } public void LoadBossData() { if (!File.Exists(Application.persistentDataPath + "/save.boss.data")) { using (BinaryReader reader = new BinaryReader(File.OpenRead(Application.persistentDataPath + "/save.boss.data"))) { woodenBotDefeated = reader.ReadBoolean(); GameManager.Instance.woodenBotDefeated = woodenBotDefeated; } } else { Debug.Log("boss doesnt exist"); } } }
October 19, 2024 at 8:35 pm #16115::just saw ur fix on youtube.
it didnt work for me but i just want to send my updated code. the same error occurs as shown above
removing the bugged peice of code allows my game to run but doesnt spawn the boss
SpawnBoss script:
using System.Collections; using System.Collections.Generic; using UnityEngine; public class spawnBoss : MonoBehaviour { public static spawnBoss Instance; [SerializeField] Transform spawnPoint; [SerializeField] GameObject boss; [SerializeField] Vector2 exitDirection; [SerializeField] GameObject bossRubble; bool callOnce; BoxCollider2D col; private void Awake() { if(WoodenBot.Instance != null) { Destroy(WoodenBot.Instance); callOnce = false; col.isTrigger = true; } if (GameManager.Instance.woodenBotDefeated == true) { callOnce = true; } if(Instance != null && Instance != this) { Destroy(gameObject); } else { Instance = this; } } // Start is called before the first frame update void Start() { col = GetComponent<BoxCollider2D>(); } // Update is called once per frame void Update() { } void OnTriggerEnter2D(Collider2D _other) { if (_other.CompareTag("Player") && !callOnce && !GameManager.Instance.woodenBotDefeated) { StartCoroutine(walkIntoRoom()); callOnce = true; } } IEnumerator walkIntoRoom() { StartCoroutine(PlayerController.Instance.WalkIntoNewScene(exitDirection, 1)); PlayerController.Instance.pState.cutscene = true; yield return new WaitForSeconds(5f); col.isTrigger = false; Destroy(bossRubble); Instantiate(boss, spawnPoint.position, Quaternion.identity); } public void isNotTrigger() { col.isTrigger = true; } }
this code in the spawnboss script still causes an error (the line that causes the error is the first on – the if statemtn conditions):
if (GameManager.Instance.woodenBotDefeated == true) { callOnce = true; }
gameManager script:
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.SceneManagement; using UnityEngine.UI; using System.IO; public class GameManager : MonoBehaviour { public string transitionedFromScene; public Vector2 platformingRespawnPoints; public Vector2 respawnPoints; [SerializeField] SavePoint savePoint; public bool woodenBotDefeated = false; [SerializeField] private FadeUI pauseMenu; [SerializeField] private float fadeTime; public bool gameIsPaused; [SerializeField] AudioClip caveMusic; AudioSource audioSource; private void Start() { audioSource = GetComponent<AudioSource>(); audioSource.clip = caveMusic; StartCoroutine(LoopAudio()); } IEnumerator LoopAudio() { float length = audioSource.clip.length; while (true) { audioSource.Play(); yield return new WaitForSeconds(length); } } private void CaveMusic() { audioSource.PlayOneShot(caveMusic); } public static GameManager Instance { get; private set; } private void Awake() { SaveData.Instance.Initialize(); if(Instance != null && Instance != this) { Destroy(gameObject); } else { Instance = this; } SaveScene(); DontDestroyOnLoad(gameObject); savePoint = FindObjectOfType<SavePoint>(); SaveData.Instance.LoadBossData(); } public void SaveScene() { string currentSceneName = SceneManager.GetActiveScene().name; SaveData.Instance.sceneNames.Add(currentSceneName); } private void Update() { if (Input.GetKeyDown(KeyCode.P)) { SaveData.Instance.SavePlayerData(); } if (Input.GetKeyDown(KeyCode.Escape) && !gameIsPaused) { pauseMenu.fadeUIIn(fadeTime); Time.timeScale = 0; gameIsPaused = true; } } public void UnpauseGame() { Time.timeScale = 1; gameIsPaused = false; } public void RespawnPlayer() { SaveData.Instance.LoadSavePoint(); if(SaveData.Instance.savePointSceneName != null) { SceneManager.LoadScene(SaveData.Instance.savePointSceneName); } if(SaveData.Instance.savePointPos != null) { respawnPoints = SaveData.Instance.savePointPos; } PlayerController.Instance.transform.position = respawnPoints; StartCoroutine(UIManager.Instance.DeactiveDeathScreen()); PlayerController.Instance.Respawned(); } }
save data script:
using System.Collections; using System.Collections.Generic; using UnityEngine; using System.IO; using UnityEngine.SceneManagement; [System.Serializable] public struct SaveData { public static SaveData Instance; //maps public HashSet<string> sceneNames; //savePoints public string savePointSceneName; public Vector2 savePointPos; //player public int playerHealth; public float playerMana; public Vector2 playerPosition; public string lastScene; public bool playerUnlockedWallJump; public bool playerUnlockedDash; public bool playerUnlockedDoubleJump; public bool playerunlockedUpSpellExplosion; public bool playerunlockedHeal; public int playerHeartsShards; public int playerManaOrbs; public int playerOrbShards; public float playerOrb0fill, playerOrb1fill, playerOrb2fill; //Bossses public bool woodenBotDefeated; public void Initialize() { if (!File.Exists(Application.persistentDataPath + "/save.savePoint.data")) { BinaryWriter writer = new BinaryWriter(File.Create(Application.persistentDataPath + "/save.savePoint.data")); } if (!File.Exists(Application.persistentDataPath + "/save.player.data")) { BinaryWriter writer = new BinaryWriter(File.Create(Application.persistentDataPath + "/save.player.data")); } if (!File.Exists(Application.persistentDataPath + "/save.boss.data")) { BinaryWriter writer = new BinaryWriter(File.Create(Application.persistentDataPath + "/save.boss.data")); } if (sceneNames == null) { sceneNames = new HashSet<string>(); } } public void SaveSavePoint() { using (BinaryWriter writer = new BinaryWriter(File.OpenWrite(Application.persistentDataPath + "/save.savePoint.data"))) { writer.Write(savePointSceneName); writer.Write(savePointPos.x); writer.Write(savePointPos.y); } } public void LoadSavePoint() { if (!File.Exists(Application.persistentDataPath + "/save.savePoint.data")) { using (BinaryReader reader = new BinaryReader(File.OpenRead(Application.persistentDataPath + "/save.savePoint.data"))) { savePointSceneName = reader.ReadString(); savePointPos.x = reader.ReadSingle(); savePointPos.y = reader.ReadSingle(); } } } public void SavePlayerData() { using (BinaryWriter writer = new BinaryWriter(File.OpenWrite(Application.persistentDataPath + "/save.player.data"))) { playerHealth = PlayerController.Instance.Health; writer.Write(playerHealth); playerHeartsShards = PlayerController.Instance.heartShards; writer.Write(playerHeartsShards); playerOrbShards = PlayerController.Instance.orbShards; writer.Write(playerOrbShards); playerManaOrbs = PlayerController.Instance.manaOrbs; writer.Write(playerManaOrbs); playerOrb0fill = PlayerController.Instance.manaOrbsHandler.orbFills[0].fillAmount; writer.Write(playerOrb0fill); playerOrb1fill = PlayerController.Instance.manaOrbsHandler.orbFills[1].fillAmount; writer.Write(playerOrb1fill); playerOrb2fill = PlayerController.Instance.manaOrbsHandler.orbFills[2].fillAmount; writer.Write(playerOrb2fill); playerMana = PlayerController.Instance.Mana; writer.Write(playerMana); playerUnlockedWallJump = PlayerController.Instance.unlockWallJump; writer.Write(playerUnlockedWallJump); playerUnlockedDash = PlayerController.Instance.unlockDash; writer.Write(playerUnlockedDash); playerUnlockedDoubleJump = PlayerController.Instance.unlockDoubleJump; writer.Write(playerUnlockedDoubleJump); playerunlockedUpSpellExplosion = PlayerController.Instance.unlockUpSpellExplosion; writer.Write(playerunlockedUpSpellExplosion); playerunlockedHeal = PlayerController.Instance.unlockHeal; writer.Write(playerunlockedHeal); playerPosition = PlayerController.Instance.transform.position; writer.Write(playerPosition.x); writer.Write(playerPosition.y); lastScene = SceneManager.GetActiveScene().name; writer.Write(lastScene); } } public void LoadPlayerData() { if (File.Exists(Application.persistentDataPath + "/save.player.data")) { using (BinaryReader reader = new BinaryReader(File.OpenRead(Application.persistentDataPath + "/save.player.data"))) { playerHealth = reader.ReadInt32(); playerHeartsShards = reader.ReadInt32(); playerOrbShards = reader.ReadInt32(); playerManaOrbs = reader.ReadInt32(); playerOrb0fill = reader.ReadSingle(); playerOrb1fill = reader.ReadSingle(); playerOrb2fill = reader.ReadSingle(); playerMana = reader.ReadSingle(); playerUnlockedWallJump = reader.ReadBoolean(); playerUnlockedDash = reader.ReadBoolean(); playerUnlockedDoubleJump = reader.ReadBoolean(); playerunlockedUpSpellExplosion = reader.ReadBoolean(); playerunlockedHeal = reader.ReadBoolean(); playerPosition.x = reader.ReadSingle(); playerPosition.y = reader.ReadSingle(); lastScene = reader.ReadString(); SceneManager.LoadScene(lastScene); PlayerController.Instance.transform.position = playerPosition; PlayerController.Instance.Health = playerHealth; PlayerController.Instance.heartShards = playerHeartsShards; PlayerController.Instance.orbShards = playerOrbShards; PlayerController.Instance.Mana = playerMana; PlayerController.Instance.manaOrbs = playerManaOrbs; PlayerController.Instance.manaOrbsHandler.orbFills[0].fillAmount = playerOrb0fill; PlayerController.Instance.manaOrbsHandler.orbFills[1].fillAmount = playerOrb1fill; PlayerController.Instance.manaOrbsHandler.orbFills[2].fillAmount = playerOrb2fill; PlayerController.Instance.unlockWallJump = playerUnlockedWallJump; PlayerController.Instance.unlockDash = playerUnlockedDash; PlayerController.Instance.unlockDoubleJump = playerUnlockedDoubleJump; PlayerController.Instance.unlockUpSpellExplosion = playerunlockedUpSpellExplosion; PlayerController.Instance.unlockHeal = playerunlockedHeal; } } else { Debug.Log("file doesnt exist"); PlayerController.Instance.Health = PlayerController.Instance.maxHealth; PlayerController.Instance.heartShards = 0; PlayerController.Instance.orbShards = 0; PlayerController.Instance.manaOrbs = 0; PlayerController.Instance.Mana = 0.5f; PlayerController.Instance.unlockWallJump = false; PlayerController.Instance.unlockDash = false; PlayerController.Instance.unlockDoubleJump = false; PlayerController.Instance.unlockUpSpellExplosion = false; PlayerController.Instance.unlockHeal = false; } } public void SaveBossData() { if (!File.Exists(Application.persistentDataPath + "/save.boss.data")) //if file doesnt exist, well create the file { BinaryWriter writer = new BinaryWriter(File.Create(Application.persistentDataPath + "/save.boss.data")); } using (BinaryWriter writer = new BinaryWriter(File.OpenWrite(Application.persistentDataPath + "/save.boss.data"))) { woodenBotDefeated = GameManager.Instance.woodenBotDefeated; writer.Write(woodenBotDefeated); } } public void LoadBossData() { if (!File.Exists(Application.persistentDataPath + "/save.boss.data")) { using (BinaryReader reader = new BinaryReader(File.OpenRead(Application.persistentDataPath + "/save.boss.data"))) { woodenBotDefeated = reader.ReadBoolean(); GameManager.Instance.woodenBotDefeated = woodenBotDefeated; } } else { Debug.Log("boss doesnt exist"); } } }
October 20, 2024 at 12:35 am #16116::through debug logs ive uncovered that the gamemanager is destroying itself cuz its instance is set to null for some reason??? i saw some previous posts and adjusted my code accordingly:
same error persists!!!
updated savedata script:
using System.Collections; using System.Collections.Generic; using UnityEngine; using System.IO; using UnityEngine.SceneManagement; [System.Serializable] public struct SaveData { public static SaveData Instance; //maps public HashSet<string> sceneNames; //savePoints public string savePointSceneName; public Vector2 savePointPos; //player public int playerHealth; public float playerMana; public Vector2 playerPosition; public string lastScene; public bool playerUnlockedWallJump; public bool playerUnlockedDash; public bool playerUnlockedDoubleJump; public bool playerunlockedUpSpellExplosion; public bool playerunlockedHeal; public int playerHeartsShards; public int playerManaOrbs; public int playerOrbShards; public float playerOrb0fill, playerOrb1fill, playerOrb2fill; public void Initialize() { if (!File.Exists(Application.persistentDataPath + "/save.savePoint.data")) { BinaryWriter writer = new BinaryWriter(File.Create(Application.persistentDataPath + "/save.savePoint.data")); } if (!File.Exists(Application.persistentDataPath + "/save.player.data")) { BinaryWriter writer = new BinaryWriter(File.Create(Application.persistentDataPath + "/save.player.data")); } if (File.Exists(Application.persistentDataPath + "/save.boss.data")) //if file doesnt exist, well create the file { File.Delete(Application.persistentDataPath + "/save.boss.data"); } if (sceneNames == null) { sceneNames = new HashSet<string>(); } } public void SaveSavePoint() { using (BinaryWriter writer = new BinaryWriter(File.OpenWrite(Application.persistentDataPath + "/save.savePoint.data"))) { writer.Write(savePointSceneName); writer.Write(savePointPos.x); writer.Write(savePointPos.y); } } public void LoadSavePoint() { if (!File.Exists(Application.persistentDataPath + "/save.savePoint.data")) { using (BinaryReader reader = new BinaryReader(File.OpenRead(Application.persistentDataPath + "/save.savePoint.data"))) { savePointSceneName = reader.ReadString(); savePointPos.x = reader.ReadSingle(); savePointPos.y = reader.ReadSingle(); } } } public void SavePlayerData() { using (BinaryWriter writer = new BinaryWriter(File.OpenWrite(Application.persistentDataPath + "/save.player.data"))) { playerHealth = PlayerController.Instance.Health; writer.Write(playerHealth); playerHeartsShards = PlayerController.Instance.heartShards; writer.Write(playerHeartsShards); playerOrbShards = PlayerController.Instance.orbShards; writer.Write(playerOrbShards); playerManaOrbs = PlayerController.Instance.manaOrbs; writer.Write(playerManaOrbs); playerOrb0fill = PlayerController.Instance.manaOrbsHandler.orbFills[0].fillAmount; writer.Write(playerOrb0fill); playerOrb1fill = PlayerController.Instance.manaOrbsHandler.orbFills[1].fillAmount; writer.Write(playerOrb1fill); playerOrb2fill = PlayerController.Instance.manaOrbsHandler.orbFills[2].fillAmount; writer.Write(playerOrb2fill); playerMana = PlayerController.Instance.Mana; writer.Write(playerMana); playerUnlockedWallJump = PlayerController.Instance.unlockWallJump; writer.Write(playerUnlockedWallJump); playerUnlockedDash = PlayerController.Instance.unlockDash; writer.Write(playerUnlockedDash); playerUnlockedDoubleJump = PlayerController.Instance.unlockDoubleJump; writer.Write(playerUnlockedDoubleJump); playerunlockedUpSpellExplosion = PlayerController.Instance.unlockUpSpellExplosion; writer.Write(playerunlockedUpSpellExplosion); playerunlockedHeal = PlayerController.Instance.unlockHeal; writer.Write(playerunlockedHeal); playerPosition = PlayerController.Instance.transform.position; writer.Write(playerPosition.x); writer.Write(playerPosition.y); lastScene = SceneManager.GetActiveScene().name; writer.Write(lastScene); } } public void LoadPlayerData() { if (File.Exists(Application.persistentDataPath + "/save.player.data")) { using (BinaryReader reader = new BinaryReader(File.OpenRead(Application.persistentDataPath + "/save.player.data"))) { playerHealth = reader.ReadInt32(); playerHeartsShards = reader.ReadInt32(); playerOrbShards = reader.ReadInt32(); playerManaOrbs = reader.ReadInt32(); playerOrb0fill = reader.ReadSingle(); playerOrb1fill = reader.ReadSingle(); playerOrb2fill = reader.ReadSingle(); playerMana = reader.ReadSingle(); playerUnlockedWallJump = reader.ReadBoolean(); playerUnlockedDash = reader.ReadBoolean(); playerUnlockedDoubleJump = reader.ReadBoolean(); playerunlockedUpSpellExplosion = reader.ReadBoolean(); playerunlockedHeal = reader.ReadBoolean(); playerPosition.x = reader.ReadSingle(); playerPosition.y = reader.ReadSingle(); lastScene = reader.ReadString(); SceneManager.LoadScene(lastScene); PlayerController.Instance.transform.position = playerPosition; PlayerController.Instance.Health = playerHealth; PlayerController.Instance.heartShards = playerHeartsShards; PlayerController.Instance.orbShards = playerOrbShards; PlayerController.Instance.Mana = playerMana; PlayerController.Instance.manaOrbs = playerManaOrbs; PlayerController.Instance.manaOrbsHandler.orbFills[0].fillAmount = playerOrb0fill; PlayerController.Instance.manaOrbsHandler.orbFills[1].fillAmount = playerOrb1fill; PlayerController.Instance.manaOrbsHandler.orbFills[2].fillAmount = playerOrb2fill; PlayerController.Instance.unlockWallJump = playerUnlockedWallJump; PlayerController.Instance.unlockDash = playerUnlockedDash; PlayerController.Instance.unlockDoubleJump = playerUnlockedDoubleJump; PlayerController.Instance.unlockUpSpellExplosion = playerunlockedUpSpellExplosion; PlayerController.Instance.unlockHeal = playerunlockedHeal; } } else { Debug.Log("file doesnt exist"); PlayerController.Instance.Health = PlayerController.Instance.maxHealth; PlayerController.Instance.heartShards = 0; PlayerController.Instance.orbShards = 0; PlayerController.Instance.manaOrbs = 0; PlayerController.Instance.Mana = 0.5f; PlayerController.Instance.unlockWallJump = false; PlayerController.Instance.unlockDash = false; PlayerController.Instance.unlockDoubleJump = false; PlayerController.Instance.unlockUpSpellExplosion = false; PlayerController.Instance.unlockHeal = false; } } public bool woodenBotDefeated; public void SaveBossData() { if (!File.Exists(Application.persistentDataPath + "/save.boss.data")) //if file doesnt exist, well create the file { BinaryWriter writer = new BinaryWriter(File.Create(Application.persistentDataPath + "/save.boss.data")); } using (BinaryWriter writer = new BinaryWriter(File.OpenWrite(Application.persistentDataPath + "/save.boss.data"))) { woodenBotDefeated = GameManager.Instance.woodenBotDefeated; writer.Write(woodenBotDefeated); } } public void LoadBossData() { if (File.Exists(Application.persistentDataPath + "/save.Boss.data")) { using (BinaryReader reader = new BinaryReader(File.OpenRead(Application.persistentDataPath + "/save.boss.data"))) { woodenBotDefeated = reader.ReadBoolean(); GameManager.Instance.woodenBotDefeated = woodenBotDefeated; } } else { Debug.Log("Boss doesnt exist"); } } }
October 20, 2024 at 12:49 am #16117::ok theres a slight development
if i click the play button, ill get the same error as usual but sometimes if i click the play button a few times itll just work and the game will work and my boss will actually spawn in. HOWEVER, if i defeat the boss, my game stops and i automatically exit play mode and am shown this error:
the code remains the same as above
October 22, 2024 at 12:37 am #16134::Hi, I suggest replacing the game manager prefab in that specific scene, if that does not work, try starting the game from another scene
October 22, 2024 at 1:22 am #16135::yep so i attempted both methods and i ended up with a different error?
seemingly something is wrong in my savedata script shown above
October 22, 2024 at 2:34 pm #16136::That new error could be cause by LoadPlayerData having a nullreference, so its unable to finish the stream
try these things:
-Save the game while playing and try again.
-Send which line is 182.
-Try commenting out parts of the load data and find which line is causing the issueOctober 22, 2024 at 11:13 pm #16142::ok so ive tried commenting out the errors and the lines which cause problems are:
in the playercontroller script:
void start() { SaveData.Instance.LoadPlayerData(); }
and in the THKevents script – the line i commented out is the problem:
void DestroyAfterDeath() { spawnBoss.Instance.isNotTrigger(); WoodenBot.Instance.DestroyAfterDeath(); GameManager.Instance.woodenBotDefeated = true; // SaveData.Instance.SaveBossData(); SaveData.Instance.SavePlayerData(); }
October 23, 2024 at 2:07 am #16148::also line 182 in the savedata script is the commented line:
public void LoadPlayerData() { if (File.Exists(Application.persistentDataPath + "/save.player.data")) { using (BinaryReader reader = new BinaryReader(File.OpenRead(Application.persistentDataPath + "/save.player.data"))) { //lastScene = reader.ReadString(); } } }
October 24, 2024 at 8:44 pm #16166 -
AuthorPosts
- You must be logged in to reply to this topic.
Advertisement below: