我目前無法從另一個(gè)腳本中獲取我的枚舉值這是我處理枚舉的腳本TrafficLightHandler.cspublic enum TRAFFIC_LIGHT{ GREEN, YELLOW, RED};public class TrafficLightHandler : MonoBehaviour { public TRAFFIC_LIGHT Trafficlight;public IEnumerator TrafficLight(){ while (true) { #region Traffic light is green //traffic light 1 = green Trafficlight = TRAFFIC_LIGHT.GREEN; if(Trafficlight == TRAFFIC_LIGHT.GREEN){ TrafficLightGreenToRed (); traffic_light_signal[0].GetComponent<Renderer>().material = materials [0]; traffic_light_signal[1].GetComponent<Renderer>().material = materials[2]; //Debug.Log(Trafficlight.ToString()); } #endregion yield return new WaitForSeconds (10); #region Traffic light is yellow Trafficlight = TRAFFIC_LIGHT.YELLOW; if(Trafficlight == TRAFFIC_LIGHT.YELLOW){ TrafficLightYellowTrafficLight1 (); traffic_light_signal[0].GetComponent<Renderer>().material = materials[1]; //Debug.Log(Trafficlight.ToString()); } #endregion yield return new WaitForSeconds(3); #region Traffic light is red Trafficlight = TRAFFIC_LIGHT.RED; if(Trafficlight == TRAFFIC_LIGHT.RED){ TrafficLightRedToGreen (); traffic_light_signal[0].GetComponent<Renderer>().material = materials [2]; traffic_light_signal[1].GetComponent<Renderer>().material = materials[0]; //Debug.Log(Trafficlight.ToString()); } }}這個(gè)腳本的問題是它只獲取綠色值,如果枚舉值從GREEN變?yōu)?,YELLOW它無法獲取YELLOW值,而是仍然是綠色的。我試過這樣做
1 回答

牧羊人nacy
TA貢獻(xiàn)1862條經(jīng)驗(yàn) 獲得超7個(gè)贊
為了能夠使用其他腳本,您需要將其作為任何其他組件使用GetComponent<TCompoenent>().
如果兩個(gè)腳本都附加到同一個(gè),gameObject那么只需使用 currentgameObject
var tlh = gameObject.GetComponent<TrafficLightHandler>();
...
tlh.Trafficlight
否則,如果腳本附加到不同的對(duì)象,那么您需要對(duì)該腳本持有者的引用才能進(jìn)行實(shí)際檢索。
public GameObject otherScriptHolder;
var tlh = otherScriptHolder.GetComponent<TrafficLightHandler>();
...
tlh.Trafficlight
- 1 回答
- 0 關(guān)注
- 226 瀏覽
添加回答
舉報(bào)
0/150
提交
取消