Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by Sendatsu_Yoshimitsu · Nov 01, 2014 at 08:33 PM · c#editor-scriptingcustom inspector

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?

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image fafase · Nov 01, 2014 at 11:05 PM 0
Share

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.

2 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

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.

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Sendatsu_Yoshimitsu · Nov 01, 2014 at 11:16 PM 0
Share

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.

avatar image fafase · Nov 01, 2014 at 11:28 PM 0
Share

Are you sure you want to use EditorGUILayout? I'd think this is meant to be wrap in some Begin/End

avatar image Sendatsu_Yoshimitsu · Nov 01, 2014 at 11:30 PM 0
Share

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.

avatar image Bunny83 · Nov 02, 2014 at 02:57 AM 0
Share

@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.

avatar image Sendatsu_Yoshimitsu · Nov 02, 2014 at 05:53 AM 0
Share

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. :)

avatar image
0

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,

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Sendatsu_Yoshimitsu · Nov 01, 2014 at 11:05 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

27 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges