Quantcast
Channel: Questions in topic: "playerprefs"
Viewing all articles
Browse latest Browse all 1333

Unity2D: PlayerPrefs.HasKey

$
0
0
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(); } public void Update() { SetScoreText(); PlayerPrefs.SetInt("boomerangTextInt", score); } void SetScoreText() { text.text = " " + score; if (score <= 0) { text.text = "None"; button.GetComponent

Viewing all articles
Browse latest Browse all 1333

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>