【发布时间】:2015-10-27 09:42:11
【问题描述】:
在 Unity 中,我有
public enum inven {Food,Scissors,Nothing};
public inven held;
如何从另一个脚本访问enum,更重要的是,如何访问held 变量中包含的信息。
我尝试了单例方法:
public class Singleton : MonoBehaviour {
public static Singleton access;
public enum inven {Nothing, Scissors, Food};
public inven held;
void Awake () {
access = (access==null) ? this : access;
}
}
创建全局变量,访问者
Singleton.inven.food //or
Singleton.access.held //respectively
但是,返回“空引用异常:对象引用未设置为对象的实例”。 我也试过用这个:
public class Accessor : MonoBehaviour {
void Start() {
GameObject HeldItem = GameObject.Find("Story"); //where story is the Gameobject containing the script of the enum and variable
TextController textcontroller = Story.GetComponent<Textcontroller>(); //Where TextController is the sript containing the enum and variable
}
}
由TextController.held 等访问,返回它需要一个对象引用。这样做的正确方法是什么?
【问题讨论】:
-
我在这里回答了一个类似的问题。 stackoverflow.com/questions/33113980/…