- Home /
Static, but visible in inspector?
hey guys,
I'm trying to use a script, where I assign different GUITextures with the inspector.
Now I also want, that these GUITextures can me manipulated by other scripts. When I use static for that, I cant assign the textures to the variables.
What should I do about that?
You could work around this by having one public var : Transform that you assign the texture to, and then a static var that on start takes on the value of the public var. but this is just a workaround, curious if there's a better solution, so I'll upvote. :)
Answer by Eric5h5 · Mar 03, 2011 at 07:45 PM
You don't need variables to be static to be manipulated by other scripts, they just have to be public. And they have to be public to be assigned in the inspector, so you're all set. In fact you should generally not use static variables unless you specifically mean for only one instance of said variables to exist, since that's what static means. Static does NOT mean "global" in any way; people seem to get confused about that.
I think what he is looking for is a variable that can be manipulated by statements like "ScriptName.Variablename = bla" where the scriptname is some script thats on a completely different object. Currently, for public variables that assigned in the inspector, you still need to assign a variable as a 'scriptname' and change characters that way after getting the 'scriptname' script from the object. Static variables, as you state, are one instance variables and should be used as such (such as GUI stuff, I find). I'd also be curious how to get a public variable that can be changed easily.
@Bob5602: No, static variables are not even related to one instance. Static variables are stored in the class itself. You don't need a single instance of that class.
Answer by Bunny83 · Mar 03, 2011 at 09:21 PM
If you have a manager class that have some public variables to assign some global useable references like GUISkin, prefabs, textures you can use the singleton pattern. You have only one instance of that class and this can store everything you like. A "normal" singleton would have a private constructor to prevent that more instances of that class are created. In Unity we won't do that to enable Unity to create our Component.
In C# a singleton can be implemented like this:
public class ObjectManager : MonoBehaviour
{
// singleton
private static ObjectManager m_Instance = null;
public static ObjectManager Get()
{
if (m_Instance == null)
m_Instance = (ObjectManager)FindObjectOfType(typeof(ObjectManager));
return m_Instance;
}
// class
public GUISkin MySkin;
public Transform PlayerPrefab;
public Transform BulletPrefab;
}
This script have to be placed only once in the scene and you can assign your variables in the inspector.
In UnityScript(Javascript) it looks similar:
// singleton
private static var m_Instance : ObjectManager = null;
static function Get() : ObjectManager
{
if (m_Instance == null)
m_Instance = FindObjectOfType(ObjectManager);
return m_Instance;
}
// class
var MySkin : GUISkin;
var PlayerPrefab : Transform;
var BulletPrefab : Transform;
Now, in any script you can use the variables very easy:
// some examples
GUILayout.Label("text", ObjectManager.Get().MySkin.label);
Instantiate(ObjectManager.Get().PlayerPrefab);
Instantiate(ObjectManager.Get().BulletPrefab);
GUI.skin = ObjectManager.Get().MySkin;
Your answer
Follow this Question
Related Questions
Reduce Draw call for Multiple GUI Textures with same Texture 1 Answer
guitexture a static var? 2 Answers
Find 2D Texture via Script to use in Static Function 2 Answers
How to make a custom GUITexture at runtime? 2 Answers
Removing a GUITexture a set amount of time after it has been enabled. 1 Answer