Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
295 views
in Technique[技术] by (71.8m points)

c# - 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

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

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.

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...