2016-10-18 34 views
0

我正在做的是在我的級別上使用Time.deltaTime將計時器從50減少到0,這很好。現在在PlayerControl腳本上,當我拾起一枚硬幣時,我希望定時器在3秒內添加。我的代碼如下。在我的定時器上添加秒

//當我拿起PlusOrb對象時,這是PlayerControl。

public class PlayerCTRL : MonoBehaviour 
{ 

    public Text timerText; 
    public TimerCount timerScript; 
    public Image DamagedOverlay; 
    public float speed; 
    public Text CountText; 
    private int count; 
    public GameObject TakeDamage; 
    public Text scoreText; 
    public TEMPLE_score scoreScript; 
    public TEMPLE_GlobalSettings globalSettings; 

    void Start() 
    { 
     CountText.text = CoinCounter.ToString(); 
     playerLife = 3; 
     count = 0; 
     SetCountText(); 
     playerAS = GetComponent<AudioSource>(); 
     timerScript = GetComponent<TimerCount>(); 
     damaged = false; 
    } 

    void OnTriggerEnter2D(Collider2D other) 
    { 
     if (other.gameObject.CompareTag("PlusOrb")) 
     { 
      Destroy(other.gameObject); 
      count = count + 1; 
      SetCountText(); 
      //score 
      scoreScript.myscore += 2; 
      scoreText.text = scoreScript.myscore.ToString(); 
      //timer 
      timerScript.timer + 3f;// this is the problem i am having 
      PlayerPrefs.SetInt("CoinCount", PlayerPrefs.GetInt("CoinCount") + 1); 
     } 
    } 
} 

//這是定時器腳本。

public class TimerCount : MonoBehaviour 
{ 
    public Text TimeText; 
    public float timer1 = 50; 
    float SceneTimer = 0; 
    TEMPLE_GlobalSettings globalSettings; 
    public Sprite lives0; 
    public GameObject Gore; 
    public PlayerCTRL PlayerController; 
    int ouch; 


    void Start() 
    { 
     timer1 = 50; 
    } 


    void Update() 
    { 
     this.GetComponent<Text>().text = timer1.ToString("F0"); 
     timer1 -= Time.deltaTime; 

     print(timer1); 
     if (timer1 < 1) 
     { 
      timer1 = 0; 
      PlayerController.playerLife = 0; 
      SceneTimer += Time.deltaTime; 
      //if (SceneTimer > 2) 
      //{ 
      //SceneManager.LoadScene("TEMPLE"); 
      //} 
     } 
    } 

    void GameOver() 
    { 
     GameObject thisGore = Instantiate(Gore, transform.position, transform.rotation) as GameObject; 
     thisGore.GetComponent<ParticleSystem>().Play(); 
     GameObject.Find("Lives").GetComponent<Image>().sprite = lives0; 
     Destroy(gameObject); 
    } 
} 

回答

0

更換

timerScript.timer + 3;// this is the problem i am having 

timerScript.timer += 3f; 

或者

timerScript.timer = timerScript.timer + 3f; 

這是一個basic C#的東西。在使用Unity之前,您應該學習C#。那裏有很多教程。

編輯

隨着你的問題的更新腳本,沒有變量的TimerCount腳本命名爲timerTimerCount腳本中的類似變量名稱爲timer1。您應該將其重命名爲timertimerScript.timer1 += 3f;

在一天結束時,這意味着您需要學習基本的C#。請不要把這當成侮辱。有必要了解基本的C#,否則你會問這樣的更類似的問題。您正在訪問的變量是未聲明的。

+0

因爲這是我的嘗試我認爲它會工作,但它仍然無法正常工作。謝謝你的答案。 –

+0

什麼不行?不要只是說它不起作用....告訴我們什麼是行不通的 – Programmer

+0

我嘗試了所有這些,現在它顯示紅色線的計時器timerScript.timer = timerScript.timer + 3f;或timerScript.timer + = 3f; –