我现在很难从另一个脚本中获得枚举的值--这是我的处理枚举的脚本
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之后更改枚举值。这是我的第二个剧本。
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值,而是仍然是绿色的。
我试过这么做
public TrafficLightHandler tlc = new TrafficLightHandler();通过这样做来呼唤我的爱
if(tlc.Trafficlight = TRAFFIC_LIGHT.GREEN)但还是一样
有人能帮我解决这个问题吗。
发布于 2019-03-13 10:32:57
为了能够使用其他脚本,您需要使用GetComponent<TCompoenent>()作为任何其他组件来检索它。
如果两个脚本都附加到同一个gameObject,那么只需使用当前的gameObject
var tlh = gameObject.GetComponent<TrafficLightHandler>();
...
tlh.Trafficlight否则,如果脚本附加到不同的对象,那么您需要引用该脚本的持有者来进行实际的检索。
public GameObject otherScriptHolder;
var tlh = otherScriptHolder.GetComponent<TrafficLightHandler>();
...
tlh.Trafficlighthttps://stackoverflow.com/questions/55139204
复制相似问题