我对代码进行了一些更改,没有错误,触发器会破坏timepickup
对象,但不会添加时间。
我喜欢调用函数addtime
或taketime
,并在需要时使用触发器方法添加或减去。
这是我的新代码:
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"));
}
}
答案 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)
Invoke("addTime")
。您收到no overload method
错误,因为您忘记添加延迟部分:Invoke(“addTime”, 3f )
答案 2 :(得分:0)
坦克大家为了你的耐心我终于得到了脚本工作其实我已经将脚本放到主摄像头,我只是从主摄像头中删除这个组件并放置在播放器上,现在它的工作非常完美。