Unity3d在倒数计时器上增加时间?

时间:2017-09-07 21:18:26

标签: c# unity3d timer

我想在播放器点击PowerUp时在计时器上添加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);
    }
}

1 个答案:

答案 0 :(得分:1)

在您提供的代码段中,您永远不会更改PlayerPrefs&#34;计时器&#34;除了&#34; 1&#34;所以你总是最终将+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");
}