我正在尝试创建一个win条件脚本,从另一个脚本中提取枚举的状态,然后用它来处理它。
crowd.cs
public enum crowdOptions {None, TeamA, TeamB};
public crowdOptions Crowd;
Crowd = crowdOption.None;
我让人群做了很多东西,但我们可以说它没有设置。
winning.cs
if (Crowd = crowdOption.None){
do something
} else if (Crowd = crowdOption.TeamA){
do something
} else {
do something
}
我尝试了一个GetComponent并将Crowd的结果设置为newvariable,但我不认为我做得对
public CrowdSway = GameObject.Find("crowdManager").GetComponent<CrowdManager>();
我也试过
if (CrowdManager.Crowd = crowdOptions.None) {
print("none");
} else {
print("hmmmmmm");
}
也不起作用。
答案 0 :(得分:1)
为了从另一个脚本访问crowd.cs类中的Crowd enum变量,该脚本需要有一个Crowd对象的实例。例如:
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
}
}
}
或者,您也可以将crowdOptions枚举变量设为静态。然后,您可以按名称从任何脚本访问它。
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
}
}
}