Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Int is 1 but shows 0?! [Fixed, fine now]

Discussion in 'Scripting' started by TaleOf4Gamers, May 7, 2016.

  1. TaleOf4Gamers

    TaleOf4Gamers

    Joined:
    Nov 15, 2013
    Posts:
    825
    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!
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.SceneManagement;
    3. using System.Collections;
    4.  
    5. /*
    6. * This is static so that I can store information/ functions in here that would
    7. * otherwise clutter other scripts.
    8. * Things that don't rely on Start(), Update(), Awake() etc can go in here to help keep thing clean and out of sight.
    9. */
    10. public static class PlayerManager : object
    11. {
    12.     /*
    13.      * the currency string is used to store the name of the currency that I want to use.
    14.      * This is easy to edit on the ClickManager.
    15.      */
    16.     public static string currency;
    17.  
    18.     /*
    19.      * These are some static variables that other scripts will
    20.      * want to access so that they can refernce and add to it.
    21.      */
    22.     public static float count = 0;
    23.     public static float perClick = 1;
    24.     public static float perSecond = 0;
    25.  
    26.     /*
    27.      * I placed these in here to try and keep the other scripts clean.
    28.      * It also makes them easy to refernce by simple using
    29.      * PlayerManager.SaveGame() etc.
    30.      */
    31.  
    32.     /*
    33.      * Save another way?
    34.      * Could save to a text file and then encrypt/decrypt it when needed
    35.      * I don't know how this would affect the performance of autosaving having to encrypt/decrypt every 30 seconds.
    36.      */
    37.     public static void SaveGame()
    38.     {
    39.         PlayerPrefs.SetFloat("count", count);
    40.         PlayerPrefs.SetFloat("perClick", perClick);
    41.         PlayerPrefs.SetFloat("perSecond", perSecond);
    42.         Debug.Log("Game Saved!");
    43.     }
    44.  
    45.     public static void LoadGame()
    46.     {
    47.         count = PlayerPrefs.GetFloat("count");
    48.         perClick = PlayerPrefs.GetFloat("perClick");
    49.         perSecond = PlayerPrefs.GetFloat("perSecond");
    50.         Debug.Log("Save Loaded!");
    51.     }
    52.  
    53.     public static void DeleteSave()
    54.     {
    55.         PlayerPrefs.DeleteKey("count");
    56.         PlayerPrefs.DeleteKey("perClick");
    57.         PlayerPrefs.DeleteKey("perSecond");
    58.         SceneManager.LoadScene("Development");
    59.         Debug.Log("Save Deleted!, scene re-loaded");
    60.     }
    61. }
    And here is a script which accesses it:
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3. using System.Collections;
    4.  
    5. public class ClickManager : MonoBehaviour
    6. {
    7.     /*
    8.      * Here is some information that I want to make easilly customizable in the future
    9.      * These are simple thing like:
    10.      * -Currency
    11.      * -Decimal places to show
    12.      *
    13.      * -Any others?
    14.      * TODO,
    15.      * Think of more customizable things!
    16.      */
    17.     [Header("Currency information")]
    18.     public string currency = "Space Bux";
    19.     public int decimalPlaces = 1;
    20.  
    21.     [Header("Text References")]
    22.     public Text countText;
    23.     public Text perClickText;
    24.     public Text perSecondText;
    25.  
    26.     void Start()
    27.     {
    28.         PlayerManager.currency = currency;
    29.         /*
    30.          * We call PerSecondCalculated 100 times a second instead of every fram so that it is consistent
    31.          * This can easilly be adjusted to perhaps 60 times a second?
    32.          */
    33.         InvokeRepeating("PerSecondCalculated", 0.01f, 0.01f);
    34.     }
    35.  
    36.     void Update()
    37.     {
    38.         Debug.Log(PlayerManager.perClick);
    39.         countText.text = PlayerManager.currency + ": " + PlayerManager.count.ToString("F" + decimalPlaces);
    40.         /*
    41.          * TODO,
    42.          * Update the Per Click and Per Second texts when an upgrade/item is bought.
    43.          * This does not need to be updated every frame!
    44.          * Low priority!
    45.         */
    46.         perClickText.text = PlayerManager.currency + "/Click: " + PlayerManager.perClick;
    47.         perSecondText.text = PlayerManager.currency + "/Second: " + PlayerManager.perSecond;
    48.     }
    49.  
    50.     public void Clicked()
    51.     {
    52.         PlayerManager.count += PlayerManager.perClick;
    53.     }
    54.  
    55.     public void PerSecondCalculated()
    56.     {
    57.         /*
    58.          * We only need to run this when the player is actually getting some
    59.          * money per second.
    60.          */
    61.         if(PlayerManager.perSecond > 0)
    62.         {
    63.             float perSecond = PlayerManager.perSecond/100;
    64.             PlayerManager.perSecond += perSecond;
    65.         }
    66.         else if (PlayerManager.perSecond < 0)
    67.         {
    68.             /*
    69.              * This is just in case.
    70.              * The player should never be getting negative money per second.
    71.              * However, if they do, this will let you know so that you can diagnose.
    72.              */
    73.             Debug.LogError("Somehow the player is getting " + PlayerManager.perSecond + " a second. It should not be negative!");
    74.         }
    75.     }
    76. }
    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.
     
  2. TaleOf4Gamers

    TaleOf4Gamers

    Joined:
    Nov 15, 2013
    Posts:
    825
    This is where I am debug logging to check the number that is being passed.
    it prints 0!
     
  3. TaleOf4Gamers

    TaleOf4Gamers

    Joined:
    Nov 15, 2013
    Posts:
    825
    For now I have manually set it to 1 via Start() but it is still very odd and would love to fix it properly.
     
  4. TaleOf4Gamers

    TaleOf4Gamers

    Joined:
    Nov 15, 2013
    Posts:
    825
    Nevermind, I found out that I saved the variable as 0.