如何从另一个脚本(Unity)中获取枚举值

时间:2019-03-13 10:08:21

标签: c# unity3d

我目前无法从另一个脚本获取枚举的值,这是我处理枚举的脚本

TrafficLightHandler.cs

public 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());
        }
        #endregion

        yield return new WaitForSeconds (10);

        //SWITCH TO SECOND TRAFFIC LIGHT
        #region Traffic light is yellow
        Trafficlight = TRAFFIC_LIGHT.YELLOW;
        if(Trafficlight == TRAFFIC_LIGHT.YELLOW){
            TrafficLightYellowTrafficLight2();
            traffic_light_signal [1].GetComponent<Renderer> ().material = materials [1];
            //Debug.Log(Trafficlight.ToString());
        }
        #endregion

        yield return new WaitForSeconds (3);
    }
  }
}

在上面的脚本上,它更改new waitforsecond之后的枚举值。现在,这是我的第二个脚本。

StopAndGoHandler.cs

TRAFFIC_LIGHT tlh;
private void TrafficLightSignal(){
    Debug.Log (tlh.ToString());
    if(tlh == TRAFFIC_LIGHT.GREEN){
        Debug.Log ("You can go");
    }
    if(tlh == TRAFFIC_LIGHT.RED){
        Debug.Log ("You need to stop");
    }
    if(tlh == TRAFFIC_LIGHT.YELLOW){
        Debug.Log ("Preparation to stop");
    }
}

此脚本的问题在于,它仅获取绿色值,并且如果枚举值从GREEN变为YELLOW,则无法获取YELLOW值,而是仍然是绿色。

我尝试这样做

 public TrafficLightHandler tlc = new TrafficLightHandler();

并通过此操作调用我的枚举

 if(tlc.Trafficlight = TRAFFIC_LIGHT.GREEN)

但还是一样

有人可以帮我吗?

1 个答案:

答案 0 :(得分:0)

要使用其他脚本,您需要使用GetComponent<TCompoenent>()将其作为其他组件进行检索。

如果两个脚本都附加到同一gameObject,则只需使用当前的gameObject

var tlh = gameObject.GetComponent<TrafficLightHandler>();
...
tlh.Trafficlight

否则,如果将脚本附加到其他对象,则需要在实际检索中引用该脚本的所有者。

public GameObject otherScriptHolder;
var tlh = otherScriptHolder.GetComponent<TrafficLightHandler>();
...
tlh.Trafficlight