2015-10-31 80 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