2016-03-04 79 views
1
void Update() 
{ 
    if (currentTarget != null) 
    { 
     this.Invoke("Shoot(currentTarget)", 0.3f); 
    } 
} 

void Shoot(Collider currentTarget) 
{ 
    ....... 
} 

我希望快速调用拍摄方法。但我得到的是尝试在统一中调用方法。不能调用方法

Trying to Invoke method: Tower.Shoot(currentTarget) couldn't be called. 

什么可以是问题?

+0

什么语言? C#或Javascript?拍摄功能在哪里?在另一班还是同班? – Programmer

+0

C#,在同一个班级 – David

+0

已更新我的回答 – Programmer

回答

3

不能调用来调用参数。这应该工作,如果你从您的拍摄功能删除参数。

Invoke("Shoot", 3f); 

然后你的拍摄功能应该是这样的

void Shoot(){ 
} 

,而不是

void Shoot(string...parameter){ 
} 

您的评论后,有另一种方式来做到这一点。这需要“协同程序”。

IEnumerator Shoot(Collider currentTarget, float delayTime) 
    { 
     yield return new WaitForSeconds(delayTime); 
     //You can then put your code below 
     //......your code 
    } 

你不能直接调用它。例如,你不能这样做: Shoot(currentTarget, 1f);

如果你不喜欢使用StartCoroutine您必须使用**StartCoroutine**(Shoot(currentTarget, 1f));

void Start() 
{ 
    //Call your function 
    StartCoroutine(Shoot(currentTarget, 1f)); 
} 

而且,那么你可以调用内的另一个功能正常的协程功能。我想你可能会喜欢这种方法使整个代码看起来应该像下面的东西:

//Changed the name to **ShootIEnum** 
IEnumerator ShootIEnum(Collider currentTarget, float delayTime=0f) 
    { 
     yield return new WaitForSeconds(delayTime); 
     //You can then put your code below 
     //......your code 
    } 

//You call this function 
void Shoot(Collider currentTarget, float delayTime=0f) 
{ 
StartCoroutine(ShootIEnum(currentTarget, 1f)); 
} 

void Update() 
    { 
     if (currentTarget != null) 
     { 
      Shoot(currentTarget, 0.3f); 
     } 
    } 

现在,任何时候你想打电话拍摄,你可以现在没有问题致电Shoot(currentTarget, 1f);

+0

但是,如果我必须使用参数调用Shoot,该怎么办?任何其他方式而不是使用Invoke? – David

+1

是的。我刚刚更新了它。我展示了如何从Start函数调用它。你不能直接调用它。您必须使用“StartCoroutine”。 StartCoroutine(拍摄(currentTarget,1f)); – Programmer

+0

无论如何,我认为这会产生错误的效果。 在我的代码更新期间,我检查目标是否还活着,然后拍摄。但问题是它开始连续拍摄。我想我有一些逻辑问题。你怎么想 ? – David

相关问题