0
votes

I'm using playerprefs to save data through out scenes. Although I'm having troubles with saving this data when the application is closed. You see I have a IAP shop that gives the player a boomerang when they purchase one, the boomerang effect (done inside my script) is activated through a button. My problem is, is that playerprefs.haskey isn't saving my boomerang effect when I close the game and then reopening it. Although it does save my boomerang effect when through scenes. This is my script:

public bool forceActive = false; 
public GameObject BoomerangOn, BoomerangOff;
public static int buttonCount = 0;
static int timesActivated =  0;

void Start()
{
    if (PlayerPrefs.HasKey ("boomerangbutton")) {
        buttonCount = PlayerPrefs.GetInt ("boomerangbutton");
        BoomerangEffect();
    }
}

void Update()
{
    PlayerPrefs.SetInt("boomerangbutton", buttonCount);
}

public void Activated ()
{
    if(timesActivated < BoomeerangText.score)
    {
        timesActivated++;
        StartCoroutine(BoomerangEffect());
    }
}

IEnumerator BoomerangEffect()
{

        BoomerangOn.SetActive (true);
        yield return new WaitForSeconds (10.0f);
        BoomerangOn.SetActive (false);
        BoomerangOff.SetActive (true);
        yield return new WaitForSeconds (1f);
        BoomerangOff.SetActive (false);
        forceActive = false;
    }

Second Edit

Okay I research a bit and linked up boomerang effect script with my boomerang text script. When the user purchase a boomerang from my IAP store, they will get 5 boomerangs, once clicked on, the boomerang text int will go down (like 5, 4, 3, 2 and 1 ) and so will my buttoncount int(that is why the timesactivaed is needed). However I change the Activated function to:

 public void Activated ()
   {
    if (timesActivated < BoomeerangText.score) {
            timesActivated++;
            StartCoroutine (BoomerangEffect ());
    }
}

So far it works regarding activating my boomerang effect when the application is closed, but when it gets to the last int (1) nothing happens, my effect doesn't takes place, so far this is my only problem. Above is an updated version of what my code looks like now. And below is my Boomerang text script:

public static int score = 0;        // The player's score.
public static int click = 1;
public GameObject button;


Text text;                      // Reference to the Text component.

// Use this for initialization
void Start()
{
    if (PlayerPrefs.HasKey ("boomerangTextInt")) {
        score = PlayerPrefs.GetInt("boomerangTextInt");
    }
} 

void Awake()
{
    text = GetComponent<Text>(); 
}

public void Update()
{
    SetScoreText();
    PlayerPrefs.SetInt("boomerangTextInt", score);
}

void SetScoreText()
{
    text.text = " " + score;
    if (score <= 0)
    {
        text.text = "None";
        button.GetComponent<Button>().interactable = false;
    }
    else if (score >= 1)
    {
        button.GetComponent<Button>().interactable = true;
    }  
    // Set the displayed text to be the word "Score" followed by the score value.
}
public void MinusBoomerangText()
{
    score -= click;
    text.text = " " + score;
}

}

And in my purchasing script I have this:

        public int scoreValue = 5;

        if (String.Equals(args.purchasedProduct.definition.id, PRODUCT_5_BOOMERANG, StringComparison.Ordinal))
        {
        BoomerangEffect.buttonCount += 5;
            BoomerangText.score += scoreValue;
            Debug.Log("Purchase successfull");
        }

Thank you.:)

1
Just as an observation - perhaps you should only be writing to your PlayerPrefs when a given value changes, and not every Update() call (which seems very excessive). - Serlite

1 Answers

0
votes

You are not calling .Save() which means all changes to PlayerPrefs are only in memory and are not persisted to disk, which means the next time you start the application all previous changes are lost.

Try the following in your save function.

void Update()
{
    PlayerPrefs.SetInt("boomerangbutton", buttonCount);
    PlayerPrefs.Save();
}

Disclaimer : I am not suggesting this is something you should do in your Update at all, as this in inefficient, but this is the root cause of your problem