尝试运行协同程序时,我不断收到错误

OmB*_*TeR -3 c# unity-game-engine

我是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;
    }

}  
}
Run Code Online (Sandbox Code Playgroud)

我将继续研究并更好地理解这一点,但我无法弄清楚这一点,我将不胜感激

I.B*_*I.B 5

你没有正确地调用你的协程

StartCoroutine(FireRate)();
Run Code Online (Sandbox Code Playgroud)

应该这样写

StartCoroutine(FireRate());
Run Code Online (Sandbox Code Playgroud)

Coroutines也可以使用它们的名称作为字符串来调用

StartCoroutine("FireRate");
Run Code Online (Sandbox Code Playgroud)

您通常应该使用第一个变体.两者之间的区别在StartCoroutine的文档中进行了解释

但是,使用字符串方法名称的StartCoroutine允许您将StopCoroutine与特定方法名称一起使用.缺点是字符串版本具有更高的运行时开销来启动协程,并且您只能传递一个参数.

您可以通过阅读此处的文档告知自己更多关于协同程序的信息.