I am having problems with my code. When I save my game and click the new day button, my day counter will go up then come back down to its saved state in 1 second and the purchase items isPurchased bools does not return to false. The day count is supposed to stay incremented and increase with every button click, and the isPurchased bools are supposed to return to false if true with every button click. This problem only happens with the day counter and isPurchased bools, not with the money counter, streetCred counter, or hasTakenOver bools. Here is the code from the GameManager script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
public class GameManager : MonoBehaviour
{
LoadGame loadGame;
PurchaseItemsGlobal pig;
public Text dayText;
public Text moneyText;
public Text streetCredText;
public static int dayCount = 1;
public static long money = 1000;
public static int streetCred = 0;
Canvas canvas;
public GameObject purchaseItemsPanel;
public PurchaseItemsGlobal purchaseItems;
public bool[] hasTakenOver;
public City[] city;
public string newDayScene;
public string exitScene;
public string winGameScene;
// Start is called before the first frame update
void Start()
{
loadGame = GameObject.FindObjectOfType<LoadGame>();
SceneManager.sceneLoaded += OnSceneLoaded;
canvas = GameObject.Find("GameCanvas").GetComponent<Canvas>();
pig = canvas.GetComponentInChildren<PurchaseItemsGlobal>(true);
if (loadGame.LoadGameManager() != null)
{
dayCount = loadGame.LoadGameManager().dayCount;
money = loadGame.LoadGameManager().money;
streetCred = loadGame.LoadGameManager().streetCred;
hasTakenOver = loadGame.LoadGameManager().hasTakenOver;
pig.isPurchased = loadGame.LoadGameManager().isPurchased;
}
SetDayText();
SetMoneyText();
SetStreetCredText();
}
private Save CreateSaveGameObject()
{
Save save = new Save();
save.dayCount = dayCount;
save.money = money;
save.streetCred = streetCred;
save.hasTakenOver = hasTakenOver;
save.isPurchased = pig.isPurchased;
return save;
}
public void SaveGame()
{
Save save = CreateSaveGameObject();
BinaryFormatter bf = new BinaryFormatter();
FileStream file = File.Create(Path());
bf.Serialize(file, save);
file.Close();
}
public string Path()
{
string path = Application.persistentDataPath + "/gamemanagersave.dat";
return path;
}
void OnSceneLoaded(Scene scene, LoadSceneMode mode)
{
if (scene.name == "CitiesPage")
{
canvas.gameObject.SetActive(true);
}
}
// Update is called once per frame
void Update()
{
WinGame();
if(Input.GetKeyDown(KeyCode.N))
{
NewDay();
}
SetDayText();
if (hasTakenOver[8] == true)
{
SaveGame();
}
}
public void SetDayText()
{
dayText.text = "Day
" + dayCount;
}
public void SetMoneyText()
{
moneyText.text = "Money: $" + money;
}
public void SetStreetCredText()
{
streetCredText.text = "Street Cred: " + streetCred;
}
public void NewDayButton()
{
NewDay();
}
public void PurchaseItemsButton()
{
purchaseItemsPanel.SetActive(true);
}
public void ExitButton()
{
SceneManager.LoadScene(exitScene);
}
void NewDay()
{
SceneManager.LoadScene(newDayScene);
dayCount++;
for(int i = 0; i < city.Length; i++)
{
city*.energy = 3;*
city*.hasEnergy = true;*
}
for(int i = 0; i < purchaseItems.isPurchased.Length; i++)
{
purchaseItems.isPurchased = false;
}
}
public void WinGame()
{
if(hasTakenOver[8] && Input.GetMouseButtonDown(0))
{
SceneManager.LoadScene(winGameScene);
}
}
}
If you need any other information, please let me know. Thank you for any help that you can offer to me.