如何从另一个脚本获取枚举值(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
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
变为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)
但还是一样
有人可以帮我吗?
推荐答案
要使用其他脚本,您需要使用GetComponent<TCompoenent>()
将其作为任何其他组件进行检索.
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屋!