简体   繁体   English

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

[英]How to get enum values from another script (Unity)

I'm currently having a trouble of getting the value of my enum from another script here's my script that handles the enum 我目前无法从另一个脚本获取枚举的值,这是处理枚举的脚本

TrafficLightHandler.cs 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);
    }
  }
}

On the script above it changes the enum value after the new waitforsecond . 在上面的脚本中,它在new waitforsecond之后更改枚举值。 Now here's my second script. 现在,这是我的第二个脚本。

StopAndGoHandler.cs 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");
    }
}

The problem with this script is that it only gets the value GREEN only and if the enum value changes like from GREEN to YELLOW it couldn't get the YELLOW value but instead still green. 该脚本的问题在于,它仅仅获取值GREEN,并且如果枚举值从GREEN变为YELLOW则无法获取YELLOW值,而是仍然为绿色。

I've tried doing this 我尝试这样做

 public TrafficLightHandler tlc = new TrafficLightHandler();

and call my enum by doing this 并通过此操作来调用我的枚举

 if(tlc.Trafficlight = TRAFFIC_LIGHT.GREEN)

but still the same 但还是一样

Could someone please help me with this. 有人可以帮我这个忙。

To be able to use the other script you need to retrieve it as any other component using GetComponent<TCompoenent>() . 为了能够使用其他脚本,您需要使用GetComponent<TCompoenent>()将其作为其他任何组件进行检索。

If both scripts are attached to the same gameObject then just use current gameObject 如果两个脚本都附加到相同的gameObject则只需使用当前的gameObject

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

Otherwise, if scripts are attached to different object then you need the reference to the holder of that script to do actual retrieval. 否则,如果将脚本附加到其他对象,则需要引用该脚本的所有者来进行实际检索。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM