c# - 如何从另一个脚本中获取枚举值(Unity)
问题描述
我目前无法从另一个脚本中获取我的枚举值这是我处理枚举的脚本
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)
但还是一样
有人可以帮我解决这个问题。
解决方案
为了能够使用其他脚本,您需要将其作为任何其他组件使用GetComponent<TCompoenent>()
.
如果两个脚本都附加到同一个,gameObject
那么只需使用 currentgameObject
var tlh = gameObject.GetComponent<TrafficLightHandler>();
...
tlh.Trafficlight
否则,如果脚本附加到不同的对象,那么您需要对该脚本持有者的引用才能进行实际检索。
public GameObject otherScriptHolder;
var tlh = otherScriptHolder.GetComponent<TrafficLightHandler>();
...
tlh.Trafficlight
推荐阅读
- php - 在我的 API 中实现 elasticsearch-php 客户端的建议功能
- jquery - 如何在codeigniter示例中使用foreach($data as $buff)进行双循环首先选择目的地,第二种形式选择是出发
- python - Pyhon3.4 TypeError:'dict_keys'对象不支持索引
- r - 使用 purrr 中的 map() 绘制时间序列
- android - 如何设置随机渐变背景颜色?
- php - 如果我们有数组索引的概念,如何使用 array_column 函数从数组中获取记录?
- division - 如何有效地找到整数 k 的值,其中 q 有限地除 b ^ k?
- c# - 如何等到 C# 中的方法完成?
- asp.net - ASP.Net UserControls 不触发任何 AutoPost Back 或 Click 事件
- python - 在熊猫中选择包含关键字的列名称