- Home /
EditorGUILayout - Use of unassigned local variable
I have a custom class UISprite (from NGUI), a variable of type UISprite in my equipmentSlot class, and I am trying to expose that variable in my custom inspector, so I can set it by dragging and dropping in edit mode. To that end, in OnInspectorGUI I've written
UISprite equipmentSprite = EditorGUILayout.ObjectField (equipmentSprite,typeof(UISprite),true);
However, this produces an error, to wit "Use of unassigned local variable equipmentSprite". Is my syntax completely off, or am I just using GUILayout incorrectly?
I edited the title to make it more relevant for later searches. Fact is I had that issue before so I would guess others will be getting it too.
Answer by fafase · Nov 01, 2014 at 11:02 PM
In you original script, try to set the corresponding variable to null in the declaration:
UISprite equipmentSprite = null;
I would think that the editor mode doe snot initialized the references like runtime does, so your reference is garbage until you would drag something into it. So when the editor script tries to read it it finds the garbage and throws the exception you mentioned.
Hmm, this half-fixes it. Right now I have:
UISprite equipmentSprite = null;
equipmentSprite = (UISprite)EditorGUILayout.ObjectField( equipmentSprite , typeof(UISprite), true);
By initializing it I can now drag things into the inspector line (before it was grayed out), but they don't stay- after I release a UISprite in the window it remains empty, and setting it with the wheel to the right of the inspector field does the same thing.
Are you sure you want to use EditorGUILayout? I'd think this is meant to be wrap in some Begin/End
I'm not married to EditorGUILayout, I was just under the quite possibly mistaken impression that it was the best way to expose things in the inspector- could you expand on what you mean by Begin/End?
To clarify my end of things, what I'm trying to figure out is how to give my EquipSlot class (which lives in each equipment slot in the inventory screen) a reference to the UISprite that holds the image of the equipped item on the character. Whenever an item is equipped or unequipped, it messages my class and asks it to change the sprite. Thus, I need to expose my UISprite in the inspector so I can give each equipment slot a reference to which sprite it changes every time an item messages it.
@Sendatsu_Yoshimitsu: Uhm you don't need any custom inspector to be able to assign your sprite reference to a variable of your EquipSlot class. Just make sure the variable is public. It's totally unclear what you actually doing here. We need more information about how your EquipSlot class looks like (at least what variables it has and which one you want to edit) and a bit more about your OnInspectorGUI method.
If you really think that you need a custom inspector, make sure you take a look at the documentation and you actually understand the relationship between the instance of your editor class and the instance of your target class (which is probably "EquipSlot"). Watch out, the sample code in the documentation is only available in UnityScript, even when you change the language.
I took your advice and got it working by simply disabling the custom inspector, although at some point in the future I do want to pound through the documentation and figure out how the heck customizing the inspector works. For clarity, what I was trying to do through the inspector (and accomplished by just switching my custom script off) was assign a reference to a UISprite variable in my EquipSlot class, so that EquipSlot could see one specific sprite in the menu without having to search for it. I made it work by just changing over to a public variable in a standard inspector window and drag-dropping. :)
Answer by Ubiquit0us · Nov 01, 2014 at 10:39 PM
Hey There,
You just have to break down your code to find out why it's not working.
UISprite equipmentSprite
1) You are saying I am making a new UISprite which by default is null [you made the container but not the object that is inside the container]
EditorGUILayout.ObjectField (equipmentSprite,typeof(UISprite),true);
2) If you notice you are using equipmentSprite which as you look from the first step is empty or better put null.
The poor function has no idea what equipmentSprite is since you never set it (ObjectFeild can't do anything with a null object). This is why you get the error.
You should do this.
[CustomEditor(typeof(UISprite))]
public class MyClass : Editor
{
private void OnInspectorGUI()
{
UISprite sprite = target as UISprite;
UISprite = EditorGUILayout.ObjectFeild( sprite , typeof(UISprite), true);
}
}
You will notice that I am using "target". The way you have it set up right now (if it did work)it would save the UISprite to the inspector class. This information would get lost as soon as you click off the object since this class is destroyed. You have to save everything to your target class (this is the class that the inspector is drawing for)
Regards,
Thank you for the clarification! I have a quick follow-up if you don't $$anonymous$$d: this produces an error, "The left-hand side of an assignment must be a variable, a property, or an indexer"- with a little tinkering I fixed this by changing the second line to
equipmentSprite = (UISprite)EditorGUILayout.ObjectField( equipmentSprite , typeof(UISprite), true);
This fixes the error and lets it compile, but while I can now see the field in my inspector, I can't set it, either by drag & dropping or manually assigning a sprite to it- have I messed my configuration up somehow?
Your answer
Follow this Question
Related Questions
Updating object on inspector value changes in editor 1 Answer
Filtering project window to ONLY selected objects 0 Answers
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Unity 5 CustomEditor for Folders 1 Answer
Trying to create Custom Inspector Labels and I get erros 0 Answers