Hey all,
I am having a really irritating issue that is stopping me from proceeding.
I have an int called perClick and is set to 1. It has been since the start but for some reason today it started printing/showing 0.
Here is some code:
This script holds my perClick variable!
using UnityEngine;
using UnityEngine.SceneManagement;
using System.Collections;
/*
* This is static so that I can store information/ functions in here that would
* otherwise clutter other scripts.
* Things that don't rely on Start(), Update(), Awake() etc can go in here to help keep thing clean and out of sight.
*/
public static class PlayerManager : object
{
/*
* the currency string is used to store the name of the currency that I want to use.
* This is easy to edit on the ClickManager.
*/
public static string currency;
/*
* These are some static variables that other scripts will
* want to access so that they can refernce and add to it.
*/
public static float count = 0;
public static float perClick = 1;
public static float perSecond = 0;
/*
* I placed these in here to try and keep the other scripts clean.
* It also makes them easy to refernce by simple using
* PlayerManager.SaveGame() etc.
*/
/*
* Save another way?
* Could save to a text file and then encrypt/decrypt it when needed
* I don't know how this would affect the performance of autosaving having to encrypt/decrypt every 30 seconds.
*/
public static void SaveGame()
{
PlayerPrefs.SetFloat("count", count);
PlayerPrefs.SetFloat("perClick", perClick);
PlayerPrefs.SetFloat("perSecond", perSecond);
Debug.Log("Game Saved!");
}
public static void LoadGame()
{
count = PlayerPrefs.GetFloat("count");
perClick = PlayerPrefs.GetFloat("perClick");
perSecond = PlayerPrefs.GetFloat("perSecond");
Debug.Log("Save Loaded!");
}
public static void DeleteSave()
{
PlayerPrefs.DeleteKey("count");
PlayerPrefs.DeleteKey("perClick");
PlayerPrefs.DeleteKey("perSecond");
SceneManager.LoadScene("Development");
Debug.Log("Save Deleted!, scene re-loaded");
}
}
And here is a script which accesses it:
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class ClickManager : MonoBehaviour
{
/*
* Here is some information that I want to make easilly customizable in the future
* These are simple thing like:
* -Currency
* -Decimal places to show
*
* -Any others?
* TODO,
* Think of more customizable things!
*/
[Header("Currency information")]
public string currency = "Space Bux";
public int decimalPlaces = 1;
[Header("Text References")]
public Text countText;
public Text perClickText;
public Text perSecondText;
void Start()
{
PlayerManager.currency = currency;
/*
* We call PerSecondCalculated 100 times a second instead of every fram so that it is consistent
* This can easilly be adjusted to perhaps 60 times a second?
*/
InvokeRepeating("PerSecondCalculated", 0.01f, 0.01f);
}
void Update()
{
Debug.Log(PlayerManager.perClick);
countText.text = PlayerManager.currency + ": " + PlayerManager.count.ToString("F" + decimalPlaces);
/*
* TODO,
* Update the Per Click and Per Second texts when an upgrade/item is bought.
* This does not need to be updated every frame!
* Low priority!
*/
perClickText.text = PlayerManager.currency + "/Click: " + PlayerManager.perClick;
perSecondText.text = PlayerManager.currency + "/Second: " + PlayerManager.perSecond;
}
public void Clicked()
{
PlayerManager.count += PlayerManager.perClick;
}
public void PerSecondCalculated()
{
/*
* We only need to run this when the player is actually getting some
* money per second.
*/
if(PlayerManager.perSecond > 0)
{
float perSecond = PlayerManager.perSecond/100;
PlayerManager.perSecond += perSecond;
}
else if (PlayerManager.perSecond < 0)
{
/*
* This is just in case.
* The player should never be getting negative money per second.
* However, if they do, this will let you know so that you can diagnose.
*/
Debug.LogError("Somehow the player is getting " + PlayerManager.perSecond + " a second. It should not be negative!");
}
}
}
tl;dr:
int perClick is 1 but for some reason prints 0!
Nowhere am I setting it to 0 so any help would be greatly appreciate.