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

[C#] Scripting Coin Amount For Realtime Updating

Discussion in 'Scripting' started by WyrmCS, Jul 14, 2018.

  1. WyrmCS

    WyrmCS

    Joined:
    Nov 9, 2014
    Posts:
    24
    Hey guys, I'm trying to use PlayerPrefs to store and obtain the CoinAmount on start right? Well it works in of itself, however I'm trying to get a realtime reaction from the moment you pick the coin up it adds 10 to the coinAmount. Here's the important bits of code.


    Here we are saying that if the player collides with the trigger "Coin" we do this:
    Code (csharp):
    1.  
    2.         if(triggerZone.gameObject.tag == "Coin")
    3.         {
    4.             coinAmount = coinAmount + 10;
    5.             PlayerPrefs.SetInt("coinKey", coinAmount);
    6.         }
    7.  
    Here is where I'm obtaining the coin amount:
    Code (csharp):
    1.  
    2.         #region Coin Handling
    3.         coinAmount = PlayerPrefs.GetInt("coinKey", coinAmount);
    4.         #endregion
    5.  
    So again, the answer I'm looking for is, how would I obtain this coinAmount in realtime. Before anyone says, "try using Time.DeltaTime in Update()". I've tried but thanks for the suggestion, which no one will probably suggest, it was just my thoughtless ego trying to fix a problem before coming to ask for help haha.

    Probably just a simple function that I'm overlooking but any help would be appreciated.

    Thank you!
     
  2. WyrmCS

    WyrmCS

    Joined:
    Nov 9, 2014
    Posts:
    24
    Nvm, found the fix. I was using a Awake function for the text component instead of a Update