1
在Unity訪問枚舉和變量,我有如何從另一個C#腳本Unity3d
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
等訪問,返回到它需要的對象引用。這樣做的正確方法是什麼?
我asnwered一個類似的問題在這裏。 http://stackoverflow.com/questions/33113980/how-do-i-use-variables-in-a-separate-script-in-unity3d/33123618#33123618 –