[英]Unity3d add time on countdown timer?
I want to add 10 second on timer, when Player hits PowerUp. 当播放器上电时,我想在计时器上增加10秒。 I have timer script, added on Canvas.
我有在Canvas上添加的计时器脚本。
So I tried to add time in PowerUp script (I used PlayerPrefs.SetInt
), but PlayerPrefs.GetInt
in timer script is always true, so immediately add some time on my counter.. 因此,我尝试在PowerUp脚本中添加时间(我使用了
PlayerPrefs.SetInt
),但是计时器脚本中的PlayerPrefs.GetInt
始终为true,因此请立即在我的计数器上添加一些时间。
This is timer.cs: 这是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;
}
}
}
And this is PowerUp script: 这是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);
}
}
In your code snippets provided, you never change the PlayerPrefs
"timer" to anything but "1" so you always end up adding +10 to second2
. 在你的代码片段提供,你永远不变的
PlayerPrefs
“计时器”到什么,但“1”,所以你总是最终加入+10 second2
。
Perhaps you meant to change it in the following IF statement: 也许您打算在以下IF语句中进行更改:
if (minutes == 0 && second2 == 1)
{
PlayerPrefs.SetInt("Gameover", 0);
Application.LoadLevel("gameover");
}
Change it to: 更改为:
if (minutes == 0 && second2 == 1)
{
PlayerPrefs.SetInt("timer", 0);
Application.LoadLevel("gameover");
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.