[英]Unity3d add time on countdown timer?
當播放器上電時,我想在計時器上增加10秒。 我有在Canvas上添加的計時器腳本。
因此,我嘗試在PowerUp腳本中添加時間(我使用了PlayerPrefs.SetInt
),但是計時器腳本中的PlayerPrefs.GetInt
始終為true,因此請立即在我的計數器上添加一些時間。
這是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
“計時器”到什么,但“1”,所以你總是最終加入+10 second2
。
也許您打算在以下IF語句中進行更改:
if (minutes == 0 && second2 == 1)
{
PlayerPrefs.SetInt("Gameover", 0);
Application.LoadLevel("gameover");
}
更改為:
if (minutes == 0 && second2 == 1)
{
PlayerPrefs.SetInt("timer", 0);
Application.LoadLevel("gameover");
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.