【问题标题】:Unity3d add time on countdown timer?Unity3d在倒数计时器上添加时间?
【发布时间】: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") 值以及minutessecond2 打印到控制台。

标签: c# unity3d timer


【解决方案1】:

在您提供的代码 sn-ps 中,您永远不会将 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");
}

【讨论】:

    猜你喜欢
    • 2019-06-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-25
    相关资源
    最近更新 更多