C#Unity不能隐式地将类型float转换为int
我正在尝试为数组中引用的游戏对象的颜色设置动画。像这样C#Unity不能隐式地将类型float转换为int,c#,unity3d,colors,gameobject,lerp,C#,Unity3d,Colors,Gameobject,Lerp,我正在尝试为数组中引用的游戏对象的颜色设置动画。像这样 public GameObject[] laneMat; void Update() { StartCoroutine(CountDownMat(laneMat, .3f)); } IEnumerator CountDownMat(GameObject[] laneMat, float delay) { time += Time.deltaTime; for(f
public GameObject[] laneMat;
void Update()
{
StartCoroutine(CountDownMat(laneMat, .3f));
}
IEnumerator CountDownMat(GameObject[] laneMat, float delay)
{
time += Time.deltaTime;
for(float i = 0; i < laneMat.Length; i++)
{
if (duration > time)
{
laneMat[i].GetComponent<Renderer>().material.color = Color.Lerp(startColor, endColor, time / duration);
yield return new WaitForSeconds(delay);
}
}
}
publicgameobject[]laneMat;
无效更新()
{
开始例行程序(倒计时表(laneMat,.3f));
}
IEnumerator倒计时表(GameObject[]laneMat,浮点延迟)
{
time+=time.deltaTime;
对于(浮点i=0;i时间)
{
laneMat[i].GetComponent().material.color=color.Lerp(开始颜色、结束颜色、时间/持续时间);
产生返回新WaitForSeconds(延迟);
}
}
}
我想要的是,让游戏对象按顺序改变颜色,每个对象之间有0.3秒的延迟
但是,我不断得到错误:无法隐式地将类型float转换为int,因为这一行laneMat[I].GetComponent().material.color=color.Lerp(startColor、endColor、time/duration)代码>我很难理解这个错误,因为数组是游戏对象的类型,所以不确定这里浮点到int的转换是什么
我尝试了另一种方法,用foreach更改for循环,但没有达到预期效果
如果您将循环中的索引(i)声明为float,将不胜感激。
然后将其作为float传递到数组的索引器中。这就是导致错误的原因
只需使用int,不再需要类型转换
for(int i = 0; i < laneMat.Length; i++)
{
if (duration > time)
{
laneMat[i].GetComponent<Renderer>().material.color = Color.Lerp(startColor, endColor, time / duration);
yield return new WaitForSeconds(delay);
}
}
for(int i=0;i时间)
{
laneMat[i].GetComponent().material.color=color.Lerp(开始颜色、结束颜色、时间/持续时间);
产生返回新WaitForSeconds(延迟);
}
}
Float i是您需要查看的内容。因此,您使用的是Float作为索引。在您的循环中,您有float i=0。。。改为使用int i=0,这将解决您的问题