【发布时间】:2017-09-07 21:18:25
【问题描述】:
当玩家点击 PowerUp 时,我想在计时器上增加 10 秒。 我有计时器脚本,添加在 Canvas 上。
所以我尝试在 PowerUp 脚本中添加时间(我使用了 PlayerPrefs.SetInt),但计时器脚本中的 PlayerPrefs.GetInt 总是正确的,所以立即在我的计数器上添加一些时间..
这是 timer.cs:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class timer : MonoBehaviour
{
public Text textTime;
public float second = 0;
private int minutes = 1;
public int second2 = 0;
// Use this for initialization
void Start()
{
}
// Update is called once per frame
void Update()
{
textTime.text = minutes + ":" + second2;
second -= Time.deltaTime;
second2 = (int)second;
if (second < 1)
{
minutes--;
second += 60f;
}
if (minutes == 0 && second2 == 1)
{
PlayerPrefs.SetInt("Gameover", 0);
Application.LoadLevel("gameover");
}
if ((PlayerPrefs.GetInt("timer")) == 1)
{
second2 += 10;
}
}
}
这是 PowerUp 脚本:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PowerUp : MonoBehaviour
{
void Start () {
}
Update is called once per frame
void Update () {
}
public void OnTriggerEnter(Collider other)
{
if (other.CompareTag("Player"))
{
PlayerPrefs.SetInt("timers", 1);
gameObject.SetActive(false);
}
}
【问题讨论】:
-
你尝试过一些基本的调试吗?尝试将
PlayerPrefs.GetInt("timer")值以及minutes和second2打印到控制台。