试图统一调用方法。方法无法调用

时间:2016-03-04 21:37:31

标签: c# unity3d invoke

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

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

我希望快速调用Shoot方法。但我得到的只是

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

可能是什么问题?

1 个答案:

答案 0 :(得分:3)

无法使用 参数调用调用。如果您从拍摄功能中删除参数,这应该有用。

<groupId>org.apache.maven.plugins</groupId>
                           <artifactId>maven-shade-plugin</artifactId>
                           <version>1.3.3</version>
                           <executions>
                                  <execution>
                                         <phase>package</phase>
                                         <goals>
                                                <goal>shade</goal>
                                         </goals>
                                         <configuration>
                                                <transformers>
                                                       <transformer
                                                              implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
                                                              <resource>META-INF/spring.handlers</resource>
                                                       </transformer>
                                                       <transformer
                                                              implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
                                                              <resource>META-INF/spring.schemas</resource>
                                                       </transformer>
                                                </transformers>
                                         </configuration>
                                  </execution>
                           </executions>

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

Invoke("Shoot", 3f);

而不是

void Shoot(){
}

在您发表评论后,还有另一种方法可以做到这一点。这需要&#34; Coroutine &#34;。

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**(Shoot(currentTarget, 1f));

此外,如果你不喜欢使用 StartCoroutine ,那么你可以在另一个普通函数中调用Coroutine函数。我想你可能喜欢这种方法,所以整个代码应该如下所示:

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

现在,只要您想要拨打Shoot,现在就可以毫无问题地致电 //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); } }