2017-03-23 120 views
-3

我是C#和编程的新手,而且在协同工作时遇到了困难,我之前使用过基本协议,并且没有任何问题,现在我正试图做一些非常相似的事情,但没有任何成功。从统一性当我试图运行协程时,我总是收到错误

错误消息:

参数#1' cannot convert方法组 '表达键入`System.Collections.IEnumerator'

最好重载方法用于`UnityEngine.MonoBehaviour.StartCoroutine(匹配System.Collections中。 IEnumerator的)”有一些无效参数

using System.Collections; 
using System.Collections.Generic; 
using UnityEngine; 
using System; 

public class Fire : MonoBehaviour 
{ 
public Transform firePos; 
public GameObject bullet; 
public bool fireCheck; 
public float spawnTime; 

IEnumerator FireRate() 
{ 
    while(fireCheck == true) 
    { 
     yield return new WaitForSeconds(spawnTime); 
     Instantiate(bullet, firePos.position, firePos.rotation); 
    } 
} 

void Start() 
{ 
    spawnTime = 4f; 
    StartCoroutine(FireRate)(); 
} 

void Update() 
{ 
    if (Input.GetKey(KeyCode.Space)) 
    { 
     fireCheck = true; 
    } 

} 
} 

我会继续研究和更好地理解这一点,但我实在不明白这一点,修复,将不胜感激

+2

这'StartCoroutine(FireRate)(告知自己更多的协同程序);'需要是这样的:'StartCoroutine(FireRate());' – DavidG

回答

5

你不打电话给你正确协程本

StartCoroutine(FireRate)(); 

应该这样写

StartCoroutine(FireRate()); 

协同程序也可以使用自己的名字称为串这样

StartCoroutine("FireRate"); 

你通常应该使用第一个变体。在两者之间的差异不过StartCoroutine使用字符串方法名允许您使用StopCoroutine用特定的方法名the documentation of StartCoroutine

解释。缺点是字符串版本有更高的运行时间开销来启动协程,并且只能传递一个参数。

你或许应该通过阅读the documentation here.

+0

干杯,我不我知道我没有看到我以前做过正确的事。 – OmBiEaTeR

+0

@OmBiEaTeR有时我们所有人都可以碰到它。 – CNuts

相关问题