首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何从另一个脚本获得枚举值(统一)

如何从另一个脚本获得枚举值(统一)
EN

Stack Overflow用户
提问于 2019-03-13 10:08:20
回答 1查看 1.8K关注 0票数 1

我现在很难从另一个脚本中获得枚举的值--这是我的处理枚举的脚本

TrafficLightHandler.cs

代码语言:javascript
复制
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

代码语言:javascript
复制
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值,而是仍然是绿色的。

我试过这么做

代码语言:javascript
复制
 public TrafficLightHandler tlc = new TrafficLightHandler();

通过这样做来呼唤我的爱

代码语言:javascript
复制
 if(tlc.Trafficlight = TRAFFIC_LIGHT.GREEN)

但还是一样

有人能帮我解决这个问题吗。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-03-13 10:32:57

为了能够使用其他脚本,您需要使用GetComponent<TCompoenent>()作为任何其他组件来检索它。

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

代码语言:javascript
复制
var tlh = gameObject.GetComponent<TrafficLightHandler>();
...
tlh.Trafficlight

否则,如果脚本附加到不同的对象,那么您需要引用该脚本的持有者来进行实际的检索。

代码语言:javascript
复制
public GameObject otherScriptHolder;
var tlh = otherScriptHolder.GetComponent<TrafficLightHandler>();
...
tlh.Trafficlight
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55139204

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档