我目前无法从另一个脚本中获取我的枚举值这是我处理枚举的脚本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()); } }}这个脚本的问题是它只获取绿色值,如果枚举值从GREEN变为 ,YELLOW它无法获取YELLOW值,而是仍然是绿色的。我试过这样做
1 回答
牧羊人nacy
TA贡献1862条经验 获得超7个赞
为了能够使用其他脚本,您需要将其作为任何其他组件使用GetComponent<TCompoenent>().
如果两个脚本都附加到同一个,gameObject那么只需使用 currentgameObject
var tlh = gameObject.GetComponent<TrafficLightHandler>();
...
tlh.Trafficlight
否则,如果脚本附加到不同的对象,那么您需要对该脚本持有者的引用才能进行实际检索。
public GameObject otherScriptHolder;
var tlh = otherScriptHolder.GetComponent<TrafficLightHandler>();
...
tlh.Trafficlight
- 1 回答
- 0 关注
- 192 浏览
添加回答
举报
0/150
提交
取消