如题
我尝试在unity里面设置红绿灯
以下是JS的写法,确认可以执行,但我想将它改成C#写法就发生错误了
var Red : Light;
var Green : Light;
var Yellow : Light;
function Start()
{
Yellow.enabled = false;
while(true)
{
Green.enabled = true;
Red.enabled = false;
yield WaitForSeconds(10);
Yellow.enabled = true;
Green.enabled = false;
yield WaitForSeconds(4);
Red.enabled = true;
Yellow.enabled = false;
yield WaitForSeconds (10);
}
}
正常执行如下
以下是修改过的C#写法
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Tflc : MonoBehaviour
{
Light Red;
Light Green;
Light Yellow;
// Use this for initialization
void Start()
{
Yellow.enabled = false;
while (true)
{
Green.enabled = true;
Red.enabled = false;
yield return new WaitForSeconds(10);
Yellow.enabled = true;
Green.enabled = false;
yield return new WaitForSeconds(4);
Red.enabled = true;
Yellow.enabled = false;
yield return new WaitForSeconds(10);
}
}
}
在void Start()显示说void不是Iterator接口
将它改成IEnumerator Start()后
会没有办法套用设置的light物件
如下图
想问问大家要怎么修正才可以正常执行
谢谢大家