如何从另一个脚本(Unity)获取枚举值 [英] How to get enum values from another script (Unity)

查看:129
本文介绍了如何从另一个脚本(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

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 之后更改了枚举值.现在这是我的第二个脚本.

On the script above it changes the enum value after the new waitforsecond. Now here's my second script.

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,如果枚举值从 GREEN 更改为 YELLOW,则无法获取 YELLOW 值,但仍然是绿色.

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.

我试过这样做

 public TrafficLightHandler tlc = new TrafficLightHandler();

并通过这样做调用我的枚举

and call my enum by doing this

 if(tlc.Trafficlight = TRAFFIC_LIGHT.GREEN)

还是一样

有人可以帮我解决这个问题吗.

Could someone please help me with this.

推荐答案

为了能够使用其他脚本,您需要像使用 GetComponent() 的任何其他组件一样检索它.

To be able to use the other script you need to retrieve it as any other component using GetComponent<TCompoenent>().

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

If both scripts are attached to the same gameObject then just use current 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

这篇关于如何从另一个脚本(Unity)获取枚举值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆