Unity timer count down not working when app killed android - unity3d

I'm new to unity and c#, I have a code to increase energy after 10m, I have used PlayerPrefs to do so it is working in unity but not working on mobile please help.
I'm unable to understand what exactly is causing problem in phone. Also the timer pauses when the app is not killed but just minimized on mobile. I want the timer countdown to keep going game minimized and also if killed.
using System;
using UnityEngine;
using UnityEngine.UI;
public class EnergyAdder : MonoBehaviour
{
// Start is called before the first frame update
public float waitTime = 600;
Timer timer;
public GameRoot gameRoot;
public int count;
DateTime currentDate;
DateTime oldDate;
void Start()
{
//timer = new Timer(waitTime);
float remainingTime = PlayerPrefs.GetFloat("TimeOnExit");
timer = new Timer(remainingTime);
if (PlayerPrefs.HasKey("sysString"))
{
currentDate = DateTime.Now;
long temp = Convert.ToInt64(PlayerPrefs.GetString("sysString"));
oldDate = DateTime.FromBinary(temp);
TimeSpan difference = currentDate.Subtract(oldDate);
count = (int)(difference.TotalSeconds / waitTime);
Debug.Log("time remaining " + count);
gameRoot.AddEnergy(count);
timer = new Timer(remainingTime - (float)difference.TotalSeconds);
//PlayerPrefs.DeleteKey("TimeOnExit");
//PlayerPrefs.DeleteKey("sysString");
}
}
// Update is called once per frame
void Update()
{
if (Gameroot.isFull)
{
GetComponent<Text>().text = "Full";
count = 0;
timer.refresh();
}
else
{
//Debug.Log("deltatime ************************"+ secondstime);
timer.countDown();
if (timer.isFinished())
{
timer = new Timer(waitTime);
timer.refresh();
gameRoot.AddEnergy(1);
}
UpdateClock();
}
}
void UpdateClock()
{
int seconds = ((int)timer.timeLeft) % 60;
int minutes = (int)(timer.timeLeft / 60);
GetComponent<Text>().text = minutes.ToString() + ":" + seconds.ToString("00");
}
void OnApplicationQuit()
{
PlayerPrefs.SetFloat("TimeOnExit", timer.timeLeft);
var today = DateTime.Now;
PlayerPrefs.SetString("sysString", today.ToBinary().ToString());
PlayerPrefs.Save();
}
}
Please help what is wrong in above code or what is missing.

From OnApplicationQuit
Note: iOS applications are usually suspended and do not quit. You should tick "Exit on Suspend" in Player settings for iOS builds to cause the game to quit and not suspend, otherwise you may not see this call. If "Exit on Suspend" is not ticked then you will see calls to OnApplicationPause instead.
I suspect it is just the same with modern Android where your app is also not really closed but hibernate.
So I think you could use OnApplicationPause and OnApplicationFocus
On Android, when the on-screen keyboard is enabled, it causes a OnApplicationFocus(false) event. Additionally, if you press "Home" at the moment the keyboard is enabled, the OnApplicationFocus() event is not called, but OnApplicationPause() is called instead.
and just do the same as you do in OnApplicationQuit also for OnApplicationPause(true) and OnApplicationFocus(false)
and do the same as you do in Start also in OnApplicationPause(false) and OnApplictionFocus(true) like
public class EnergyAdder : MonoBehaviour
{
public float waitTime = 600;
public GameRoot gameRoot;
public int count;
// already reference this in the Inspector
[SerializeField] private Text timerText;
private Timer timer;
private DateTime currentDate;
private DateTime oldDate;
private void Awake()
{
// store this reference ONCE
if(!timerText) timerText = GetComponent<Text>();
}
private void Start()
{
GetTime();
}
private void Update()
{
// Note: probably a typo? shouldn't it be "gameRoot" ?
if (Gameroot.isFull)
{
timerText.text = "Full";
count = 0;
timer.refresh();
}
else
{
//Debug.Log("deltatime ************************"+ secondstime);
timer.countDown();
if (timer.isFinished())
{
timer = new Timer(waitTime);
timer.refresh();
gameRoot.AddEnergy(1);
}
UpdateClock();
}
}
private void UpdateClock()
{
int seconds = ((int)timer.timeLeft) % 60;
int minutes = (int)(timer.timeLeft / 60);
timerText.text = $"{minutes}:{seconds:00}";
}
private void OnApplicationQuit()
{
StoreTime();
}
// pauseStatus is True if the application is paused, else False.
private void OnApplicationPause(bool pauseStatus)
{
if(pauseStatus)
{
StoreTime();
}
else
{
GetTime();
}
}
// hasFocus is True if the GameObjects have focus, else False.
private void OnApplicationFocus(bool hasFocus)
{
if(hasFocus)
{
GetTime();
}
else
{
StoreTime();
}
}
private void StoreTime()
{
PlayerPrefs.SetFloat("TimeOnExit", timer.timeLeft);
var today = DateTime.Now;
PlayerPrefs.SetString("sysString", today.ToBinary().ToString());
PlayerPrefs.Save();
}
private void GetTime()
{
// NOTE: You should consider to pass a default value like
//float remainingTime = PlayerPrefs.GetFloat("TimeOnExit", 15);
float remainingTime = PlayerPrefs.GetFloat("TimeOnExit");
timer = new Timer(remainingTime);
if (PlayerPrefs.HasKey("sysString"))
{
currentDate = DateTime.Now;
long temp = Convert.ToInt64(PlayerPrefs.GetString("sysString"));
oldDate = DateTime.FromBinary(temp);
TimeSpan difference = currentDate.Subtract(oldDate);
count = (int)(difference.TotalSeconds / waitTime);
Debug.Log("time remaining " + count);
gameRoot.AddEnergy(count);
timer = new Timer(remainingTime - (float)difference.TotalSeconds);
}
}
}
Note: Typed on smartphone so I can't test it but I hope it goes into the right direction

Related

Fail mechanism for a unity rythm game

Hello i know this question might be a bad one but i have tried everything to my ability to acomplish this but i cant, so i have been making a rythm game and for the life of me i have made everything else other than the fail mechanism i just cant get it to work. i need to read the variable (MissedNotes) and make the code pause the time and show text saying (Too Many Notes Missed). I have made the place to insert the text in but i cant figure out how to do it at all. Im a begginer so any help is good, Thanks for helping me.
This is the code
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.InteropServices;
using UnityEngine;
using UnityEngine.UI;
public class GameManager : MonoBehaviour
{
public AudioSource theMusic;
public AudioSource beatSfx;
public bool startPlaying;
public BeatScroller theBS;
public static GameManager instance;
public int currentScore;
public int scorePerNote = 2000;
public int scorePerGoodNote = 2500;
public int scorePerPerfectNote = 3000;
public int currentMultiplier;
public int multiplierTracker;
public int[] multiplierThresholds;
public Text scoreText;
public Text multiText;
public float totalNotes;
public float NormalHits;
public float GoodHits;
public float PerfectHits;
public float MissedHits;
public GameObject resultsScreen;
public Text percentHitText, normalsText, goodsText, perfectsText, missesText, rankText, finalScoreText;
public GameObject FAILTEXT;
// Start is called before the first frame update
void Start()
{
instance = this;
scoreText.text = "Press Any Key to Continue";
currentMultiplier = 1;
totalNotes = ((FindObjectsOfType<NoteObject>().Length) - 168);
}
// Update is called once per frame
void Update()
{
if (!startPlaying)
{
if (Input.anyKeyDown)
{
scoreText.text = "0";
startPlaying = true;
theBS.hasStarted = true;
theMusic.Play();
}
}
else
{
if (!theMusic.isPlaying && !resultsScreen.activeInHierarchy)
{
resultsScreen.SetActive(true);
normalsText.text = "" + NormalHits;
goodsText.text = "" + GoodHits;
perfectsText.text = "" + PerfectHits;
missesText.text = "" + MissedHits;
float totalHits = NormalHits + GoodHits + PerfectHits;
float percentHit = (totalHits / totalNotes) * 100f;
float totalMiss = MissedHits;
percentHitText.text = percentHit.ToString("F1") + "%";
string rankVal = "F";
if (percentHit >= 30)
{
rankVal = "D";
if (currentScore >= 550000)
{
rankVal = "C";
if (currentScore >= 750000)
{
rankVal = "B";
if (currentScore >= 900000)
{
rankVal = "A";
if (currentScore >= 950000)
{
rankVal = "S";
}
}
}
}
}
rankText.text = rankVal;
finalScoreText.text = currentScore.ToString();
}
}
}
public void NoteHit()
{
beatSfx.Play();
multiplierTracker++;
if ((currentMultiplier - 1) < multiplierThresholds.Length)
{
multiplierTracker++;
if (multiplierThresholds[currentMultiplier - 1] <= multiplierTracker)
{
multiplierTracker = 0;
currentMultiplier++;
}
}
scoreText.text = "" + currentScore;
multiText.text = "Score x" + currentMultiplier;
}
public void NormalHit()
{
UnityEngine.Debug.Log("Normal Hit");
currentScore += scorePerNote * currentMultiplier;
NormalHits++;
NoteHit();
}
public void GoodHit()
{
UnityEngine.Debug.Log("Good Hit");
currentScore += scorePerGoodNote * currentMultiplier;
GoodHits++;
NoteHit();
}
public void PerfectHit()
{
UnityEngine.Debug.Log("Perfect Hit");
currentScore += scorePerPerfectNote * currentMultiplier;
PerfectHits++;
NoteHit();
}
public void NoteMissed()
{
UnityEngine.Debug.Log("missed");
MissedHits++;
currentMultiplier = 1;
multiplierTracker = 0;
multiText.text = "Score x" + currentMultiplier;
}
}
There's nothing controlling time here so there's not really anything to do in this class to pause time. I'm also not seeing how the note hit functions are called.
It seems like there's some other class controlling the timing and note hit logic, and that's where you need to implement the logic to stop the game.
Once you've done that you can modify your NoteMissed method to trigger the game logic to stop when too many notes are missed.

RewardVideo Script Energy +1

I do not know how to Reward the player with more energy after seeing the reward ad.
So I want to reward the player with Energy.
It looks like your post is mostly code please add some more details.
It looks like your post is mostly code please add some more details.
It looks like your post is mostly code please add some more details.
private BannerView bannerAd;
private InterstitialAd interstitial;
private RewardBasedVideoAd rewardBasedVideo;
bool isRewarded = false;
// Start is called before the first frame update
void Start()
{
MobileAds.Initialize(InitializationStatus => { });
this.rewardBasedVideo = RewardBasedVideoAd.Instance;
this.rewardBasedVideo.OnAdRewarded += this.HandleRewardBasedVideoRewarded;
this.rewardBasedVideo.OnAdClosed += this.HandleRewardBasedVideoClosed;
this.RequestRewardBasedVideo();
this.RequestBanner();
}
void Update()
{
if (isRewarded)
{
isRewarded = false;
}
I have an energy bar 250/250, once press play the energy is going to 249/250 Energy, how can I script to increase the Energy +1 when player press the reward Button?
[SerializeField] Text energyText;
[SerializeField] Text timerText;
[SerializeField] Slider energyBar;
private int maxEnergy = 250;
private int currentEnergy;
private int restoreDuration = 250;
private DateTime nextEnergyTime;
private DateTime lastEnergyTime;
private bool isRestoring = false;
// Start is called before the first frame update
void Start()
{
if(!PlayerPrefs.HasKey("currentEnergy"))
{
PlayerPrefs.SetInt("currentEnergy", 50);
Load();
StartCoroutine(RestoreEnergy());
}
else
{
Load();
StartCoroutine(RestoreEnergy());
}
}
public void UseEnergy()
{
if(currentEnergy >= 1)
{
currentEnergy--;
UpdateEnergy();
if (isRestoring == false)
{
if(currentEnergy + 1 == maxEnergy)
{
nextEnergyTime = AddDuration(DateTime.Now, restoreDuration);
}
StartCoroutine(RestoreEnergy());
}
}
else
{
Debug.Log("Isufficient Energy!!");
}
}
private IEnumerator RestoreEnergy()
{
UpdateEnergyTimer();
isRestoring = true;
while(currentEnergy < maxEnergy)
{
DateTime currentDateTime = DateTime.Now;
DateTime nextDateTime = nextEnergyTime;
bool isEnergyAdding = false;
while(currentDateTime > nextDateTime)
{
if(currentEnergy < maxEnergy)
{
isEnergyAdding = true;
currentEnergy++;
UpdateEnergy();
DateTime timeToAdd = lastEnergyTime > nextDateTime ? lastEnergyTime : nextDateTime;
nextDateTime = AddDuration(timeToAdd, restoreDuration);
}
else
{
break;
}
}
if (isEnergyAdding == true)
{
lastEnergyTime = DateTime.Now;
nextEnergyTime = nextDateTime;
}
UpdateEnergyTimer();
UpdateEnergy();
Save();
yield return null;
}
isRestoring = false;
}
private DateTime AddDuration(DateTime datetime, int duration)
{
return datetime.AddSeconds(duration);
//return datetime.AddMinutes(duration);
}
private void UpdateEnergyTimer()
{
if (currentEnergy >= maxEnergy)
{
timerText.text = "Full";
return;
}
TimeSpan time = nextEnergyTime - DateTime.Now;
string timeValue = String.Format("{0:D2}:{1:D1}", time.Minutes, time.Seconds);
timerText.text = timeValue;
}
private void UpdateEnergy()
{
energyText.text = currentEnergy.ToString() + "/" + maxEnergy.ToString();
energyBar.maxValue = maxEnergy;
energyBar.value = currentEnergy;
}
private DateTime StringToDate(string datetime)
{
if (String.IsNullOrEmpty(datetime))
{
return DateTime.Now;
}
else
{
return DateTime.Parse(datetime);
}
}
private void Load()
{
currentEnergy = PlayerPrefs.GetInt("currentEnergy");
nextEnergyTime = StringToDate(PlayerPrefs.GetString("nextEnergyTime"));
lastEnergyTime = StringToDate(PlayerPrefs.GetString("lastEnergyTime"));
}
private void Save()
{
PlayerPrefs.SetInt("currentEnergy", currentEnergy);
PlayerPrefs.SetString("nextEnergyTime", nextEnergyTime.ToString());
PlayerPrefs.SetString("lastEnergyTime", lastEnergyTime.ToString());
}
}
You can determinate if the user won at ads callback events
add this event callback
// Called when the user should be rewarded for interacting with the ad.
this.rewardedAd.OnUserEarnedReward += HandleUserEarnedReward;
then
public void HandleUserEarnedReward(object sender, Reward args)
{
//Now it's time to reward the user
//Use those if you want to get the amount from the ad setup
//string type = args.Type;
//double amount = args.Amount;
}

Unity PlayerPrefs loads different data after Build the Game in WebGL

I'm using playerprefs in my WordSearch Game that I built with Webgl. The Issue is appear when I close my game during the game play for several times. It's automatically starts with random countdown value such as 20, 140 etc with previous score(Sometimes Not the exact score that should be saved).
Here's the code that I'm using playerpreds...
[SerializeField] public float currentLevelScore = 0.0f;
[SerializeField] private float timeLeft;
[SerializeField] private string accessToken = "";
void Awake()
{
GetPlayerPrefData();
}
void Start() {
if(timeLeft == fixedTime)
{
PauseGame();
}
//Retrive Saved Player prefs Data
if (PlayerPrefs.GetFloat("timeLeft")<150 && PlayerPrefs.GetFloat("timeLeft") > 0)
{
StartGameCanvas.SetActive(false);
}
else
{
if(PlayerPrefs.GetFloat("timeLeft") == 150)
{
StartGameCanvas.SetActive(true);
}
}
if (PlayerPrefs.GetFloat("timeLeft").Equals(0) || PlayerPrefs.GetFloat("timeLeft").Equals(null))
{
PlayerPrefs.SetFloat("timeLeft", fixedTime);
}
}
void GetPlayerPrefData()
{
accessToken = PlayerPrefs.GetString("accessToken");
currentLevelScore = PlayerPrefs.GetFloat("currentLevelScore");
float countdownTime = PlayerPrefs.GetFloat("timeLeft");
if (countdownTime <= 0)
{
timeLeft = fixedTime;
}
else
{
timeLeft = countdownTime;
}
}
//This function is calls when the Time is up or user hit the quit button
public void ResetAllData()
{
float defaultScore = 0;
string defaultAccessToken = null;
PlayerPrefs.SetFloat("currentLevelScore", defaultScore);
PlayerPrefs.SetFloat("timeLeft", fixedTime);
PlayerPrefs.SetString("timeLeft", defaultAccessToken);
PlayerPrefs.Save();
}
This issue is only occur after build the game. It's works fine in the editor.
Can anyone give me an idea or help to solve this problem? any suggestions would be highly appreciated...

Using a timer in conjunction with 2 push buttons from arduino

So I am using two push buttons (connected to an Arduino Uno) as an input to my game. The player has to push down both buttons at the same time for the character to move in the game. I want the player to hold down the buttons for a different amount of time in each level. I have a working Arduino and a working Unity timer and player script, but am not able to get the code to do what I want. What I basically want is that only when the player presses the buttons down, does the timer start counting down. Right now, the timer starts as soon as the scene begins. I know that I somehow have to reference the timer script to the button object, I have tried this but it still doesn't work. Note that the timer UI does have a Timer tag on it. I have also referenced the Player Controller script in the Timer script. Right now, Its giving me a range of errors. I have attached an image depicting these errors.error image
The Timer script:
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class Timer : MonoBehaviour
{
//int startTime = 0;
public bool buttonPressed = false;
public int timeLeft;
public Text countdownText;
GameObject Character;
void Awake()
{
Character = GameObject.FindWithTag("Player");
}
public void Start()
{
//StartCoroutine("LoseTime");
BeginTimer();
}
void Update()
{
countdownText.text = ("Time Left = " + timeLeft);
if (timeLeft <= 0)
{
//StopCoroutine("LoseTime");
//countdownText.text = "Times Up!";
Invoke("ChangeLevel", 0.1f);
}
}
public void BeginTimer()
{
Character.GetComponent<PlayerController>().Update();
//gameObject.GetComponent<MyScript2>().MyFunction();
if (buttonPressed == true )
{
StartCoroutine("LoseTime");
}
else if (buttonPressed == false)
{
StopCoroutine("LoseTime");
}
}
IEnumerator LoseTime()
{
while (true)
{
yield return new WaitForSeconds(1);
timeLeft--;
}
}
void ChangeLevel()
{
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1);
}
}
The Player Script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO.Ports;
public class PlayerController : MonoBehaviour
{
SerialPort sp = new SerialPort("\\\\.\\COM4", 9600);
//player == GameObject.FindWithTag("Player").GetComponent<>();
public float Speed;
public Vector2 height;
public float xMin, xMax, yMin, yMax;
public bool buttonPressed = false;
GameObject Character;
public void Awake()
{
Character = GameObject.FindWithTag("Player");
}
public void Start()
{
if (!sp.IsOpen)
{ // If the erial port is not open
sp.Open(); // Open
}
sp.ReadTimeout = 1; // Timeout for reading
}
public void Update()
{
if (sp.IsOpen)
{ // Check to see if the serial port is open
try
{
string value = sp.ReadLine();//To("Button"); //Read the information
int button = int.Parse(value);
//float amount = float.Parse(value);
//transform.Translate(Speed * Time.deltaTime, 0f, 0f); //walk
if (button == 0) //*Input.GetKeyDown(KeyCode.Space*/) //jump
{
buttonPressed = true;
Character.GetComponent<Rigidbody2D>().AddForce(height, ForceMode2D.Impulse);
Character.GetComponent<Rigidbody2D>().position = new Vector3
(
Mathf.Clamp(GetComponent<Rigidbody2D>().position.x, xMin, xMax),
Mathf.Clamp(GetComponent<Rigidbody2D>().position.y, yMin, yMax)
);
Timer tmr = GameObject.Find("Timer").GetComponent<Timer>();
tmr.BeginTimer();
}
}
catch (System.Exception)
{
}
}
void ApplicationQuit()
{
if (sp != null)
{
{
sp.Close();
}
}
}
}
}
I think the problem may be with how I am referencing the scripts in each other.
In your timer you have a quite strange mixup of Update and Coroutine. Also note that BeginTimer is called exactly once! You also shouldn't "manually" call Update of another component.
I wouldn't use Update at all here. Simply start and stop a Coroutine.
The Timer script should only do the countdown. It doesn't have to know more:
public class Timer : MonoBehaviour
{
public int timeLeft;
public Text countdownText;
private bool timerStarted;
public void BeginTimer(int seconds)
{
// Here you have to decide whether you want to restart a timer
timeLeft = seconds;
// or if you rather want to continue counting down
//if(!timerStarted) timeLeft = seconds;
StartCoroutine(LoseTime());
}
public void StopTimer()
{
StopAllCoroutines();
}
private IEnumerator LoseTime()
{
timerStarted = true;
while (timeLeft > 0)
{
yield return new WaitForSeconds(1);
timeLeft --;
countdownText.text = $"Time Left = {timeLeft}";
}
// Only reached after the timer finished and wasn't interrupted meanwhile
// Using Invoke here is a very good idea since we don't want to interrupt anymore
// if the user lets go of the button(s) now
Invoke(nameof(ChangeLevel), 0.1f);
}
void ChangeLevel()
{
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1);
}
}
In general avoid to use Find at all. If anyhow possible already reference things in the Inspector! If needed you can use Find but only once! What you never want to do is use any of the Find and GetComponent variants repeatedly - rather store the reference the first time and re-use it - and especially not in Update no a per frame basis. They are very expensive calls!
public class PlayerController : MonoBehaviour
{
public float Speed;
public Vector2 height;
// I prefer to use Vector2 for such things
public Vector2 Min;
public Vector2 Max;
public bool buttonPressed = false;
// Already reference these via the Inspector if possible!
public Rigidbody2D Character;
public Timer timer;
public Rigidbody2D _rigidbody;
private SerialPort sp = new SerialPort("\\\\.\\COM4", 9600);
private void Awake()
{
FetchReferences();
}
// This is my "secret" tip for you! Go to the component in the Inspector
// open the ContextMenu and hit FetchReferences
// This already stores the references in the according fields ;)
[ContextMenu("FetchReferences")]
private void FetchReferences()
{
if(!Character)Character = GameObject.FindWithTag("Player"). GetComponent<Rigidbody2D>();
if(!timer) timer = GameObject.Find("Timer").GetComponent<Timer>();
}
private void Start()
{
if (!sp.IsOpen)
{
sp.Open(); // Open
}
sp.ReadTimeout = 1;
}
private void Update()
{
// I wouldn't do the serialport open check here
// your if block simply silently hides the fact that your application
// doesn't work correctly! Rather throw an error!
try
{
string value = sp.ReadLine(); //Read the information
int button = int.Parse(value);
//TODO: Since it isn't clear in your question how you get TWO buttons
//TODO: You will have to change this condition in order to only fire if both
//TODO: buttons are currently pressed!
buttonPressed = button == 0;
if (buttonPressed)
{
Character.AddForce(height, ForceMode2D.Impulse);
// The clamping of a rigidbody should always be done ine FixedUpdate!
// Pass in how many seconds as parameter or make the method
// parameterless and configure a fixed duration via the Inspector of the Timer
timer.BeginTimer(3.0f);
}
else
{
// Maybe stop the timer if condition is not fulfilled ?
timer.StopTimer();
}
}
catch (System.Exception)
{
// You should do something here! At least a Log ...
}
}
private void FixedUpdate()
{
// Here I wasn't sure: Are there actually two different
// Rigidbody2D involved? I would assume you rather wanted to use the Character rigidbody again!
Character.position = new Vector3(Mathf.Clamp(Character.position.x, Min.x, Max.x), Mathf.Clamp(Character.position.y, Min.y, Max.y));
}
// Did you mean OnApplicationQuit here?
private void ApplicationQuit()
{
if (sp != null)
{
{
sp.Close();
}
}
}
}
Typed on smartphone but I hope the idea gets clear

Unity2D: PlayerPrefs.HasKey

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.:)
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