触发器销毁对象,但无法添加时间

时间:2016-11-21 02:08:48

标签: c# unity3d

我对代码进行了一些更改,没有错误,触发器会破坏timepickup对象,但不会添加时间。 我喜欢调用函数addtimetaketime,并在需要时使用触发器方法添加或减去。

这是我的新代码:

using UnityEngine;
using System.Collections;

public class CountDownTimer : MonoBehaviour {
    float timeRemaining = 60.0f;

    public void addTime()
    {
        timeRemaining += 100.00f;
    }

    public void taketime()
    {
        timeRemaining -= 10.00f;
    }

    void Update () {
        timeRemaining -= Time.deltaTime;
    }

    void OnGUI(){
        if (timeRemaining > 0) {
            GUI.Label(new Rect(325, 30, 200, 50), 
                    "Time Remaining : " +timeRemaining);
        }
        else {
            Application.LoadLevel("Game Over");
        }
    }

    void OnTriggerEnter(Collider other)
    {
        // The switch statement checks what tag the other gameobject is, and reacts accordingly.
        switch (other.gameObject.tag)
        {
            case "TimePickup":
                Invoke("addtime", 3f);
                break;
            case "TimeOut":
                Invoke("taketime", 3f);
                break;
        }
        // Finally, this line destroys the gameObject the player collided with.
        Destroy (GameObject.FindWithTag("TimePickup"));
    }
}

3 个答案:

答案 0 :(得分:2)

您是否需要使用Invoke?

请改为尝试:

 void OnTriggerEnter(Collider other)
    {
        // The switch statement checks what tag the other gameobject is, and reacts accordingly.
        switch (other.gameObject.tag)
        {
            case "TimePickup":
                addTime();
                Destroy (other.gameObject);
                break;
            case "TimeOut":
                taketime();
                break;
            default: // Invalid tag, let's hear about it
                Debug.Log("Invalid tag "+other.gameObject.tag);
                break;
        }            
    }

答案 1 :(得分:0)

凯文628回答了这个问题。您必须将函数调用重命名为Invoke("addTime")。您收到no overload method错误,因为您忘记添加延迟部分:Invoke(“addTime”, 3f

答案 2 :(得分:0)

坦克大家为了你的耐心我终于得到了脚本工作其实我已经将脚本放到主摄像头,我只是从主摄像头中删除这个组件并放置在播放器上,现在它的工作非常完美。