如題
我嘗試在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);
}
}
正常執行如下
https://imgur.com/a/8BJyo
以下是修改過的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物件
如下圖
https://imgur.com/a/BL7rq
想問問大家要怎麼修正才可以正常執行
謝謝大家