[英]how to get an enum from another script in unity
I am trying to create a win condition script pulling the status of an enum from a different script and then do stuff with it. 我试图创建一个获胜条件脚本,从另一个脚本中获取枚举的状态,然后对其进行处理。
crowd.cs
public enum crowdOptions {None, TeamA, TeamB};
public crowdOptions Crowd;
Crowd = crowdOption.None;
I have the crowd doing a bunch of stuff, but lets say it is set to none. 我有很多人在做很多事情,但是可以说它没有设置。
winning.cs
if (Crowd = crowdOption.None){
do something
} else if (Crowd = crowdOption.TeamA){
do something
} else {
do something
}
I tried a GetComponent and set the result of of Crowd to a newvariable, but I don't think I did that right 我尝试了一个GetComponent并将Crowd的结果设置为一个新变量,但是我认为我做的不正确
public CrowdSway = GameObject.Find("crowdManager").GetComponent<CrowdManager>();
I also tried 我也试过
if (CrowdManager.Crowd = crowdOptions.None) {
print("none");
} else {
print("hmmmmmm");
}
that didn't work either. 那也不起作用。
In order to access the Crowd enum variable in your crowd.cs class from another script, that script needs to have an instance of a Crowd object. 为了从另一个脚本访问您的race.cs类中的Crowd枚举变量,该脚本需要具有Crowd对象的实例。 For example:
例如:
public class Crowd : MonoBehaviour
{
public enum crowdOptions {None, TeamA, TeamB};
public crowdOptions crowdOpts;
}
public class Winning : MonoBehaviour
{
void Start()
{
Crowd myCrowd = new Crowd();
if(myCrowd.crowdOpts == crowdOptions.None)
{
//do something
}
}
}
Alternatively, you could also make your crowdOptions enum variable static. 另外,您也可以将您的全民(crowdOptions)枚举变量设为静态。 Then you can access it from any script by name.
然后,您可以按名称从任何脚本访问它。
public class Crowd : MonoBehaviour
{
public enum crowdOptions {None, TeamA, TeamB};
public static crowdOptions CrowdOptions;
}
public class Winning : MonoBehaviour
{
void Start()
{
if(CrowdOptions == Crowd.crowdOptions.None)
{
//do something
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.