- Home /
I have no idea why this isn't working
So i have got this script here and it is saving and loading data fine, thats not the problem but the realAnts value is not changing and i have no idea why, as im pretty sure all the things start repeating when level 2 loads, and there is definitely a gameManager object in the scene so anyone got an ideas why this isn't working;
using System; using System.IO; using System.Collections; using System.Collections.Generic; using System.Runtime.Serialization.Formatters.Binary; using UnityEngine;
public class GameManager : MonoBehaviour {
public int ants;
public int queens;
public int coins;
[SerializeField]
private float realAnts;
private float nl3;
private const float BR = 1 / 60;
private float multiplier;
private int currentSaveGame;
private GameObject inventory, shop, menu;
private MenuButtons[] menuButtons;
private CurrencyDisplays[] currencyDisplays;
private void Awake()
{
DontDestroyOnLoad(gameObject);
nl3 = Mathf.Log(3f);
}
private void CalculateBonus()
{
float _nlAnts = Mathf.Log(ants);
float _power = _nlAnts / nl3;
float _firstMultiplier = Mathf.Pow(1.5f, _power);
multiplier = _firstMultiplier * queens;
}
private void GiveAnts()
{
realAnts += BR * multiplier;
ants = Mathf.RoundToInt(realAnts);
foreach (CurrencyDisplays _currencyDisplay in currencyDisplays)
{
_currencyDisplay.SetText();
}
}
private void OnLevelWasLoaded(int level)
{
if (level == 2)
{
InvokeRepeating("CalculateBonus", 0f, 60f);
InvokeRepeating("GiveAnts", 0f, 1f);
InvokeRepeating("Save", 5f, 5f);
menuButtons = GameObject.FindObjectsOfType<MenuButtons>();
foreach (MenuButtons _menuButton in menuButtons)
{
_menuButton.Setup();
}
currencyDisplays = GameObject.FindObjectsOfType<CurrencyDisplays>();
foreach (CurrencyDisplays _currencyDisplay in currencyDisplays)
{
_currencyDisplay.Setup();
}
if (menu == null)
{
menu = GameObject.FindGameObjectWithTag("Menu");
shop = GameObject.FindGameObjectWithTag("Shop");
inventory = GameObject.FindGameObjectWithTag("Inventory");
}
shop.SetActive(false);
inventory.SetActive(false);
menu.SetActive(true);
}
if (level == 1)
{
CancelInvoke();
}
}
public void SetStuff()
{
ants += 10;
queens += 10;
coins += 10;
StartSave(ants, queens, coins, realAnts);
}
public void StartSave(int _ants, int _queens, int _coins, float _realAnts)
{
ants = _ants;
queens = _queens;
coins = _coins;
realAnts = _ants;
Save();
}
public void ExitToMenu()
{
CancelInvoke();
Save();
}
private void OnApplicationQuit()
{
CancelInvoke();
Save();
}
public void Save()
{
Debug.Log("Saving Data");
BinaryFormatter _bf = new BinaryFormatter();
FileStream _file = File.Create(Application.persistentDataPath + "/SaveGame" + currentSaveGame);
PlayerData _data = new PlayerData();
_data.coins = coins;
_data.queens = queens;
_data.ants = ants;
_data.realAnts = realAnts;
_bf.Serialize(_file, _data);
_file.Close();
}
public void Load(int _saveGame)
{
currentSaveGame = _saveGame;
Debug.Log("Loading Data");
if (File.Exists(Application.persistentDataPath + "/SaveGame" + _saveGame))
{
BinaryFormatter _bf = new BinaryFormatter();
FileStream _file = File.Open(Application.persistentDataPath + "/SaveGame" + _saveGame, FileMode.Open);
PlayerData _data = (PlayerData)_bf.Deserialize(_file);
_file.Close();
coins = _data.coins;
ants = _data.ants;
queens = _data.queens;
realAnts = _data.realAnts;
print(coins + " " + ants + " " + queens);
} else
{
Debug.Log("File Does Not Exist");
coins = 650;
queens = 1;
ants = 1;
realAnts = 1;
}
}
}
[Serializable] class PlayerData { public int ants; public int coins; public int queens; public float realAnts; }
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Illuminating a 3D object's edges OnMouseOver (script in c#)? 1 Answer
Check timer time when changed it. 2 Answers