- Home /
How to deactivate a UI button temporarily
I'm making a game where you are trying to get a high score. The first time you die, you have an option to watch an ad and then restart the game with the score that you died with. But I don't want people spamming this button and getting endless scores so I want to be able to deactivate it once someone has used it once. I have this script below to control a lot of things but the parts I need help with are the very last function and the if statement in the update function. Thanks.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using TMPro;
using UnityEngine.Advertisements;
using UnityEngine.SceneManagement;
public class ScoreCounter : MonoBehaviour {
public float score;
public float continueButton;
public Button advertisingButton;
public TextMeshProUGUI scoreText;
public TextMeshProUGUI highScoreText;
public float highScore;
public TextMeshProUGUI isHighScore;
string gameId = "3885078";
bool testMode = true;
private void Awake()
{
if (PlayerPrefs.HasKey("HighScore"))
{
highScore = PlayerPrefs.GetFloat("HighScore");
highScoreText.text = highScore.ToString();
}
if (PlayerPrefs.HasKey("Score"))
{
score = PlayerPrefs.GetFloat("Score");
PlayerPrefs.DeleteKey("Score");
}
}
void Start()
{
continueButton = 0f;
StartCoroutine("Repeater");
Advertisement.Initialize(gameId, testMode);
}
void Update()
{
scoreText.text = score.ToString();
highScoreText.text = highScore.ToString();
HighScore();
if (continueButton == 1f)
{
advertisingButton.gameObject.SetActive(false);
continueButton = 0f;
}
}
IEnumerator Repeater()
{
yield return new WaitForSeconds(1.2f);
InvokeRepeating("Counter", .32f, .32f);
}
void Counter()
{
score += 1f;
}
void HighScore()
{
if (score > highScore)
{
highScore = score;
PlayerPrefs.SetFloat("HighScore", highScore);
isHighScore.SetText("New High Score!");
}
if (score < highScore)
{
isHighScore.SetText("Current High Score");
}
}
public void Continue()
{
if (Advertisement.IsReady("rewardedVideo"))
{
Advertisement.Show("rewardedVideo");
Time.timeScale = 1;
PlayerPrefs.SetFloat("Score", score);
SceneManager.LoadScene("Game");
}
}
public void ContinueButton()
{
continueButton += 1f;
}
}
Answer by Hellium · Nov 01, 2020 at 02:29 PM
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using TMPro;
using UnityEngine.Advertisements;
using UnityEngine.SceneManagement;
public class ScoreCounter : MonoBehaviour
{
public float score;
public Button advertisingButton;
public TextMeshProUGUI scoreText;
public TextMeshProUGUI highScoreText;
public float highScore;
public TextMeshProUGUI isHighScore;
string gameId = "3885078";
bool testMode = true;
private void Awake()
{
if (PlayerPrefs.HasKey("HighScore"))
{
highScore = PlayerPrefs.GetFloat("HighScore");
highScoreText.text = highScore.ToString();
}
if (PlayerPrefs.HasKey("Score"))
{
advertisingButton.gameObject.SetActive(false); // Continue button has been used, disable it
score = PlayerPrefs.GetFloat("Score");
PlayerPrefs.DeleteKey("Score");
}
}
void Start()
{
StartCoroutine("Repeater");
Advertisement.Initialize(gameId, testMode);
}
void Update()
{
scoreText.text = score.ToString();
highScoreText.text = highScore.ToString();
HighScore();
}
IEnumerator Repeater()
{
yield return new WaitForSeconds(1.2f);
InvokeRepeating("Counter", .32f, .32f);
}
void Counter()
{
score += 1f;
}
void HighScore()
{
if (score > highScore)
{
highScore = score;
PlayerPrefs.SetFloat("HighScore", highScore);
isHighScore.SetText("New High Score!");
}
if (score < highScore)
{
isHighScore.SetText("Current High Score");
}
}
public void Continue()
{
if (Advertisement.IsReady("rewardedVideo"))
{
Advertisement.Show("rewardedVideo");
Time.timeScale = 1;
PlayerPrefs.SetFloat("Score", score);
SceneManager.LoadScene("Game");
}
}
}
@Hellium Thanks again. ($$anonymous$$aybe I should have you make my game for me)
Your answer
Follow this Question
Related Questions
Save score across scenes 1 Answer
2D character movement is jittery (top-down) 1 Answer
How do I flip the character when an object is infront/behind it? 1 Answer
[SOLVED]Problem with 2D Collider 0 Answers
2D Movement with axis? 1 Answer