我使用随机范围来选择产生的敌人类型之间.
IEnumerator Spawn()
{
int randAmount = UnityEngine.Random.Range(minAmount, maxAmount); // Determine quantity of enemies to be spawned
int randType = UnityEngine.Random.Range(0, 1); // Determine which enemy is spawned
enemies[randType](randAmount); // Call enemy type, then amount
yield return new WaitForSeconds(spawnRate);
StartCoroutine(Spawn()); // Loop
}
Run Code Online (Sandbox Code Playgroud)
当与调试器连接到统一时,我可以看到randAmount
每次调用协程时都会发生变化,但是randType
从0开始没有变化,我一直在测试,我开始认为它不再是运气不好.
我知道第一个变量工作正常,无论如何确保第二个变量正常工作,或者每次调用协程时获取0到1之间的随机数的替代方法.
我是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)
我将继续研究并更好地理解这一点,但我无法弄清楚这一点,我将不胜感激