• 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
Question by tneshi · Feb 26, 2012 at 07:57 AM · c#editorinspectorattribute

Some public attributes not shown in inspector, default references

In my script, I have several public attributes. Some of them are visible in the inspector, others are not. Why?

 using UnityEngine;
 using System.Collections;
 
 public class TestInspector : MonoBehaviour {
     public string str; // not visible
     public int i;      // not visible
     public float f;    // not visible
     public GameObject go;      // visible
     public AnimationClip anim; // visible
 }

I select the TestInspector script in the project. The script itself, not a GmeObject with the script attached in the Hierarchy! So I'm talking about the Default References.

When I select an object, that has the script attached, all attributes are visible in the inspector. But I want to set all the default values for the script in the inspector, not in code.

Comment
cregox
VSZM

People who like this

2 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 tneshi · Feb 26, 2012 at 09:45 AM 0
Share

Actually, I think only attributes of basic types like string, int etc. are not shown. I just added an attribute of type Animation to Interaction and it shows up.

3 Replies

· Add your reply
  • Sort: 
avatar image

Answer by Dreamora · Feb 26, 2012 at 08:40 AM

public is shown in the editor but only public instance values. Static will never show in the editor as it is not related to the instance of the monobehaviour, its a class value thats unique application wide.

If you want to expose this kind of information you must write a CustomEditor for your class

Comment
cregox

People who like this

1 Show 6 · 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 tneshi · Feb 26, 2012 at 09:09 AM 0
Share

The attributes in question are not static. There are two static attributes, but I am not asking about them, but specifically about interactionName2 and interactionName, which are instance attributes.

avatar image Dreamora · Feb 26, 2012 at 09:11 AM 0
Share

the only variables you have in there are statics beside the one shown. The rest are no variables but properties and properties are never exposed in the editor, again unless you write a customeditor

avatar image tneshi · Feb 26, 2012 at 11:12 AM 0
Share

public class Lumber : GameObjectInteraction { public GameObject trunkPrefab; public string interactionName2; //... }

How is public string interactionName2; static?

avatar image Dreamora · Feb 26, 2012 at 12:03 PM 0
Share

Its not static. And actually it is, although the naming is very badly chosen (like the property of the base class), also no property, at least thats not verifyable from the code.

So generally it should show up, unless you have an InteractionName2 as property in one of those two abstract classes but in such a case the unity console would output a warning.

For InteractionName though the property part remains due to the dual usage of the same 'editor exposure string' (InteractionName - thats what the property is called but also how the variable would be shown in the editor). Generally the question would be why that one is public at all there, as it makes the property meaningless and without the property it should actually show up there.

avatar image tneshi · Feb 26, 2012 at 12:15 PM 0
Share

Oh, the code only is as it is at the moment, because I am trying to make it show up in the inspector desperately.

I would like to have it defined publicly in the base class Interaction, without a property and not defined in the child classes and then set it per child class in the inspector, e.g. in Lumber interactionName would be "chop down" etc.

As this seems to create too much confusion, I will edit my question to make it more simple.

Show more comments
avatar image

Answer by Bunny83 · Feb 26, 2012 at 02:06 PM

Why do you use an abstract class? Usually it's better to keep the inheritance chain short. If you really need some kind of base class, use only one. For other dependencies it's better to use interfaces and define methods and properties.

Anyway, i've tested your setup and the inspector show all variables as it should:

AbstractClassChainMembers

Properties aren't serialized and therefore never show up in the inspector. The property "InteractionName" is actually a bit useless since you just wrap another public variable.

If you want to display properties, you need to implement a custom inspector for your class.

Comment
cregox

People who like this

1 Show 3 · 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 tneshi · Feb 26, 2012 at 03:38 PM 0
Share

Hi Bunny and thanks again for your answer. I already edited the question for clarifications, but it does not show up.

Thre property is useless, I just have tested different things in hopes it would show up in the inspector somehow.

I actually want to edit these fields in the Default Preferences field. I have read in the meantime, that only UnityEngine.Object can be edited there, which is a pitty. Can a custom inspector also set default preferences?

avatar image Bunny83 · Feb 26, 2012 at 07:46 PM 0
Share

That doesn't make much sense, since default references are only reference types and only those to assets in your project. string, int and float are value types. They are initialized either by the default value of this type, a field-initializer or in the constructor / Awake / Start method of the class.

Instance variables are always unique for each instance. The default references are just a little helper if a reference is not initialized (null) it got set to the default reference when an instance is created. Value-types can't be null. They always hold a value, therefore it makes no sense to have a similar feature for those.

What you actually want is a template / prefab behaviour where you change the value at one place and it affect all instances of this prefab. Prefabs also allow you to override the common "inherited" values for single instances, if you want.

avatar image tneshi · Feb 27, 2012 at 11:48 AM 0
Share

I assure you, it does make sense.

My scripts are attached to dozends, if not hundreds of prefabs. I want to manage the default values in one place. I can achieve this in the Awake method, but I would rather do it in the inspector.

I understand, that there are restrictions and intentions from Unity. But it wouldn't have to be the way they implemented it. I understand by now that they use the per script setters only for references. I don't like that and it could be otherwise. But if it's too much trouble, I will just set it in code.

avatar image

Answer by Dreamora · Feb 26, 2012 at 12:19 PM

For what you want to do the whole thing would look like the one below assuming you want to set chopdown basing on the class, not through editor (but in case of editor you wouldn't set it anywhere in code but do it on the prefab)

public class Lumber : GameObjectInteraction {

public GameObject trunkPrefab; void Awake () { interactionName = "Chop down"; } }

public abstract class GameObjectInteraction : Interaction {}

public abstract class Interaction : MonoBehaviour {

public static readonly Vector2 NULL_VECTOR2 = new Vector2(float.NaN, float.NaN); public static readonly Vector3 NULL_VECTOR3 = new Vector3(float.NaN,float.NaN,float.NaN);

protected string interactionName; private float maxDistanceSquare = 2.5f; private int importance = 1;

//... }

Comment

People who like this

0 Show 4 · 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 tneshi · Feb 26, 2012 at 12:26 PM 0
Share

I want to set it in the inspector, not in code. And I want to set it on the script, not on a prefab nor on a game object. This can be done for things like AnimationClips and GameObjects. And I wonder, why it shouldn't be possible for a string.

avatar image Dreamora · Feb 26, 2012 at 12:30 PM 0
Share

Strings will show up in the editor just fine like GOs etc do too. BUT: if it shows up in the editor and you want to set it there, you will set it on a prefab or on a gameobject, the class itself (hence the script) is completely untouched by anything you set in the editor, the code will not change due to it.

If you want to set it in the editor, just kick out the awake again and you can set the interaction name in the editor, will show up there as Interaction Name.

You make use of this all the time, cause the name of the game object is nothing but UnityEngine.Object.name string either.

avatar image tneshi · Feb 26, 2012 at 01:09 PM 0
Share

I am talking about the Default References. It seems that only attributes of type UnityEngine.Object will show up here. However, I want to edit a string here. I have a valid use case for it, but it seems that it isn't supported. I suppose, you don't know how to make it work the way I want to?

avatar image Dreamora · Feb 26, 2012 at 01:23 PM 0
Share

Default values work too, put it onto the line where you define the string and it will be there when you drop it onto a game object in the editor or add it through code.

But it logically does not work for extended classes as you don't define the field there again, there you would use Awake for it normally (which is Unitys constructor for in-class stuff only basically - start is the function to use to setup relationships to other gameobjects and components, 'out-of-class' stuff).

I don't get what you mean with it works for UnityEngine.Object stuff cause gameobject etc are NULL by default and are null in your cases above too until you fill them in the editor (thats what I meant with set it on a prefab / game object in the scene) and the same holds for public string somestring present in a monobehaviour.

But perhaps you talk about something totally different here, not the default value upon monobehaviour creation.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

6 People are following this question.

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

Related Questions

[AttributeUsage(AttributeTargets.Field)] - how do I use it? 0 Answers

Best way of getting all object instances in a scene from an editor class 0 Answers

Reset a SerializedProperty to it's default value. 2 Answers

Unity Editor - Class variable value contrain 4 Answers

Insert new custom class element with _default_ values to a SerializedProperty array? 1 Answer


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