- Home /
Game Persist code not working.
Hi, I have been trying to use these scripts for some collectible objects so they don't respawn when I reload the game, however I cannot get it to work. Here are the scripts I'm using: using System.Linq; using UnityEngine;
public class GamePersist : MonoBehaviour
{
void OnDisable() => Save();
void OnEnable() => Load();
void Load()
{
foreach (var persist in FindObjectsOfType<MonoBehaviour>(true).OfType<ISaveState>())
{
persist.Load();
}
}
void Save()
{
foreach(var persist in FindObjectsOfType<MonoBehaviour>(true).OfType<ISaveState>())
{
persist.Save();
}
}
}
internal interface ISaveState
{
void Save();
void Load();
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class cratebox : MonoBehaviour, ISaveState
{
public GameObject currencyprefab;
public GameObject Destruction;
public Transform cratepos;
private void OnTriggerEnter2D(Collider2D collision)
{
if (collision.gameObject.CompareTag("Bullet"))
{
gameObject.SetActive(false);
Instantiate(currencyprefab, cratepos.position, cratepos.rotation);
Instantiate(currencyprefab, cratepos.position, cratepos.rotation);
Instantiate(currencyprefab, cratepos.position, cratepos.rotation);
Instantiate(Destruction, cratepos.position, cratepos.rotation);
}
if (collision.gameObject.CompareTag("Stomp"))
{
gameObject.SetActive(false);
Instantiate(currencyprefab, cratepos.position, cratepos.rotation);
Instantiate(currencyprefab, cratepos.position, cratepos.rotation);
Instantiate(currencyprefab, cratepos.position, cratepos.rotation);
Instantiate(Destruction, cratepos.position, cratepos.rotation);
}
}
public string PickedUpKey => $"Crate - { gameObject.name } -PickedUp";
public void Save()
{
PlayerPrefs.SetInt(PickedUpKey, gameObject.activeSelf ? 0 : 1);
}
public void Load()
{
int isActive = PlayerPrefs.GetInt(PickedUpKey);
if(isActive ==1)
{
gameObject.SetActive(isActive == 1);
}
}
}
The cratebox script is attached to the collectible while the GamePersist is attached to another object. Supposedly when the GamePersist is disabled the cratebox's state should be saved and when the GamePersist is reenabled after resetting the game, the cratebox should be disabled if I have collected the item last game, but it is not happening.
Okay, I think I found the problem, the guy in the tutorial actually did it wrong at first and later in the video he went and fixed it.
Answer by Hellium · Mar 06 at 08:13 AM
public void Load()
{
int isActive = PlayerPrefs.GetInt(PickedUpKey);
gameObject.SetActive(isActive == 1);
}
Oh thanks, I didn't realize I made a mistake there. So far, what' s happening now is in reverse. When I load the game when the crate is collected in the last save, the crate comes back, but if I don't collect the crate and save, the crate disappears on load.
Answer by highpockets · Mar 05 at 11:09 PM
It appears that you are only ever calling the save function when you set the game object to inactive, but the way you are saving the value is by checking if the GameObject is active, but it will always be inactive because of obvious reasons, so the int will always be 1 unless you are calling save from somewhere else.
Thanks for being with me, I removed the OnDisable and OnEnabled method and tried calling them with keys, however I am still getting the same result, after I collect the item and save, the item still reappears after I load. I think I may have done something else wrong but I am going to check the tutorial I was following again.
// private void OnDisable() => Save();
// private void OnEnable() => Load();
private void Update()
{
if (Input.GetKeyDown(KeyCode.P))
{
Save();
}
if (Input.GetKeyDown(KeyCode.O))
{
Load();
}
}
Answer by DrowningGeodude · Mar 08 at 02:58 AM
Update: Okay I just reverse the 0 : 1 to 1 : 0 and it seems to be working correctly now, for some reason the guy in the tutorial thought it should be the other way around.(Originally he did go with 1 : 0 but later swapped it to 0 : 1)
Much thanks to those that contributed.
Your answer
Follow this Question
Related Questions
Keep Singleton while creating holder GameObject at runtime? 1 Answer
Make runtime-instantiated game objects persistent OR make their unique ID persistent 0 Answers
How to save custom data types 1 Answer
How to Save Objects that were added into the scene at runtime 2 Answers
Persisting data between scenes: can't find an answer 2 Answers