2016-11-21 38 views
-1

我對代碼進行了一些更改,沒有任何錯誤,觸發器銷燬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")); 
    } 
} 
+0

目前還不清楚你在這裏試圖完成什麼。你能清楚而清楚地陳述你的問題嗎?僅靠代碼很難解釋你遇到的問題。 – kevin628

+0

我喜歡要調用函數addtime或taketime,並添加或減去我需要的觸發器方法。代碼沒有錯誤,它會銷燬對象timepickup,但它根本不會增加時間。 –

+0

其代碼是用於添加時間或減去此代碼的時間代碼拾取器 –

回答

2

是有一些原因,你需要使用調用?

試試這個:

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; 
     }    
    } 
+0

否以及這裏的一位朋友告訴我試試它只是爲了增加timeRemain的時間,但我嘗試了多種方式來做但不工作。我喜歡拿起遊戲上的定時器,這將添加時間到我的定時器上CountDownTimer.cs –

+0

對象其銷燬,但沒有時間添加所有 –

0

kevin628回答了這個問題。您必須將函數調用重命名爲Invoke("addTime")。你得到了no overload method錯誤,因爲你忘了加上延遲部分:調用(「更新時間」,3F

+0

你的意思是重命名它,我需要重命名我的addTime函數。以及你告訴我,我忘記了延遲,對於這個問題感到抱歉,但我是腳本新手,你的意思是3f On Final? –

+0

函數'Invoke'有兩個參數。首先,要調用的函數,其次,調用之前等待多久。 – Hannott

+1

您應該直接調用該函數,或者確保您具有在調用中正確寫入的函數的名稱。請記住,你可能會到達GameOver,因爲在3秒延遲之前沒有添加時間 – Hannott

0

坦克大家的耐心終於得到其實我長嘆地方腳本主腳本工作相機,我只是從主相機中刪除這個組件,並放置在播放器,現在它的工作完美。