我制作了一个气球弹出游戏,我希望获得一定数量的分数奖励,例如,如果你得到10分,你得到一个贴纸,或者你得到20分,你得到一个keyholder,如果你有0分,你会再次尝试文本,所以问题是,当我完成游戏时,它会再次显示文本,然后当你打开另一个程序时(比如去文件夹或文本以外的任何文本)刷新,然后我得到正确的文本。我尝试过使用update,LateUpdate和FixedUpdate,但没有任何变化。
这是脚本:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class CountDownTimer : MonoBehaviour
{
//public string levelToLoad;
public float timer = 60f;
public Text timerSeconds;
public GameObject panelWin, score, time, pausa;
public int _playerScore;
public Text premio;
// Use this for initialization
void Start ()
{
timerSeconds = GetComponent<Text>();
time.SetActive(true);
panelWin.SetActive(false);
Time.timeScale = 1;
}
// Update is called once per frame
void LateUpdate ()
{
timer -= Time.deltaTime;
timerSeconds.text = timer.ToString("f0");
if (timer <=0)
{
if (PlayerPrefs.HasKey("Points"))
{
_playerScore = PlayerPrefs.GetInt("Points");
if (_playerScore == 0)
{
premio.text = "Intenta de nuevo!";
}
else
{
if (_playerScore >= 1 && _playerScore <= 10)
{
premio.text = "Tu premio es: una calco Bebé a bordo";
}
else
{
if (_playerScore >= 11 && _playerScore <= 20)
{
premio.text = "Tu premio es: un llavero Huggies";
}
else
{
if (_playerScore >= 21 && _playerScore <= 30)
{
premio.text = "Tu premio es: un pack de toallitas";
}
else
{
if (_playerScore >= 31 && _playerScore <= 40)
{
premio.text = "Tu premio es: un pack de
pañales";
}
else
{
if (_playerScore >= 41 && _playerScore >= 50)
{
premio.text = "Tu premio es: un bolso
Huggies";
}
}
}
}
}
}
}
panelWin.SetActive(true);
score.SetActive(false);
//time.SetActive(false);
pausa.SetActive(false);
if (panelWin == true)
{
Time.timeScale = 0;
}
}
}
public void DoARestart()
{
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
}
public void Menu()
{
SceneManager.LoadScene("TitleScreen");
}
}
这是GameController脚本:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class GameControllerScript : MonoBehaviour {
public Transform balloonPrefab;
public Text scoreDisplay;
public Text scoreDisplayWin;
public int _playerScore = 0;
//private int _multiplier = 1;
private float _timeSinceLastSpawn = 0.0f;
private float _timeToSpawn = 0.0f;
private List<Transform> _balloons;
private const int BALLOON_POOL = 30;
void Start () {
PlayerPrefs.SetInt("Points", 0);
_balloons = new List<Transform>();
for (int i = 0; i < BALLOON_POOL; i++) {
Transform balloon = Instantiate(balloonPrefab) as Transform;
balloon.parent = this.transform;
_balloons.Add(balloon);
}
SpawnBalloon();
GameStart();
}
/*void InitMultiplier() {
if (PlayerPrefs.HasKey("Multiplier")) {
_multiplier = Mathf.Max (1, PlayerPrefs.GetInt ("Multiplier"));
}
}*/
void InitPoints() {
if (PlayerPrefs.HasKey("Points"))
{
_playerScore = PlayerPrefs.GetInt("Points");
}
}
// Update is called once per frame
void Update () {
_timeSinceLastSpawn += Time.deltaTime;
if (_timeSinceLastSpawn >= _timeToSpawn)
{
SpawnBalloon();
}
}
void SpawnBalloon() {
_timeSinceLastSpawn = 0.0f;
_timeToSpawn = Random.Range (0.0f, 2.0f);
foreach (Transform b in _balloons) {
BalloonScript bs = b.GetComponent<BalloonScript>();
if (bs && !bs.isActive) {
bs.Activate();
break;
}
}
}
public void AddPoints(int points=1) {
_playerScore += points;
UpdateScoreDisplay();
}
public void GameOver() {
SavePoints();
SceneManager.LoadScene("TitleScreen");
}
void UpdateScoreDisplay() {
scoreDisplay.text = "Puntaje: " + _playerScore.ToString();// + "(x" +
_multiplier.ToString() + ")";
scoreDisplayWin.text = "Tu puntaje es: " + _playerScore.ToString();
}
public void GameStart() {
InitPoints();
//InitMultiplier();
UpdateScoreDisplay();
}
void OnApplicationPause() {
SavePoints();
}
void OnApplicationQuit() {
SavePoints();
}
void SavePoints() {
PlayerPrefs.SetInt("Points", _playerScore);
}
}
我在控制台上没有任何错误,如果有人可以提供帮助,我会很高兴!谢谢!
答案 0 :(得分:0)
是的。在CountDownTimer sudo service network-manager restart
中使用之前,您没有正确更新Points
...以下是您的计划流程:
LateUpdate()
将Points
设置为0
。Start()
(每一帧)上,检查LateUpdate()
是否从playerPrefs.HasKey("Points")
启用的GameController
开始(运行Start()
),这可能是整个第一个游戏框架,将导致true
。_playerScore
设置为Points
(0
),然后根据该值选择premio.text
的值。GameController.GameOver()
可能未在脚本/游戏的第一帧中调用,实际上可能没有调用几帧。CountDownTimer.LateUpdate()
(称为每一帧,包括第一帧),从GameControler.enabled == true
(可能是第一帧)的第一帧开始,Points
将在那里等于0
。CountDownTimer.LateUpdate()
中,您将从"Intenta de nuevo!"
(可能是整个第一帧)的第一帧获得GameControler.enabled == true
几帧,直到GameControler.GameOver()
被调用