繁体   English   中英

Unity3d在倒数计时器上添加时间吗?

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM