2015-10-31 78 views
0

我現在遇到的團結問題:統一委託轉換的問題

我儘量讓事件偵聽器與委託,和我從教程的代碼,但我有一個錯誤,當我嘗試添加代理方法轉換爲其他類方法:「不能隱式轉換類型void' to Metronome.OnTickEvent」。

這裏是我的類委託:

public delegate void OnTickEvent(); 
public event OnTickEvent onTick; 

IEnumerator coroutineMetronome() { 
    if (CustomTimer.manager.timerState) { 
     for (;;) { 
      nextTick += delay; 
      yield return new WaitForSeconds(nextTick - Time.time); 

      onTick(); // I call the delegate method here 
     } 
    } 
} 

...這是事件接收器類:

protected virtual void Start() 
{ 
    manager = this as T; 

    Metronome.manager.onTick += OnSynchronization(); // Here is the bug line 
} 

protected void OnSynchronization() { 
    Debug.Log("coucou"); 
} 

感謝您的幫助!

回答

4

你不需要的方法名稱後的括號:

Metronome.manager.onTick += OnSynchronization; 

隨着括號那豈不是要首先調用該方法,然後將結果添加到事件。

改正的代碼是

Metronome.manager.onTick += new OnTickEvent(OnSynchronization); 
+0

該死的短版我沒看出來。謝謝 – Karz