- Home /
GUI.Style referencing, one script to another
Is it possible to reference a GUI.Style i made in one Script to another? If so what is the correct way to do this.
I have tried setting my original GUI.Style variable to public:
public var myStyle :GUIStyle;
and referencing this style from my other script like this:
GUI.Box (Rect (5, 150, 100, 400), "Hello", script01.myStyle);
But get an error: "Assets/GUI/Bar_Bottom.js(42,76): BCE0020: An instance of type 'script01' is required to access non static member 'myStyle'."
If i change the variable to public static var, it crashes unity. Is this just not possible and i have to recreate the same style in each script? Or just make a GUISkin even though im only changing the look of one item.
Answer by Michael La Voie · Mar 31, 2010 at 05:54 PM
If the script you want to access is a component of the same game object as the script that will access it, you can load up the script using GetComponent()
otherscript = GetComponent(MyOtherScriptName);
GUI.Box (Rect (5, 150, 100, 400), "Hello", otherscript.myStyle);
Alternatively, if the script is part of another game object, first find that game object, then use GetComponent() to get its script
otherGameObject = GameObject.Find("OtherGameObjectName");
otherscript = otherGameObject.GetComponent(MyOtherScriptName);
GUI.Box (Rect (5, 150, 100, 400), "Hello", otherscript.myStyle);
As a third option, if you know the other game object at design time, you can create a public variable to hold it in the script that will do the accessing. Then, from Unity's property inspector, you can drag and drop the other game object onto that variable.
var otherGameObject : GameObject;