• 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
0
Question by Taranee · Jul 08, 2014 at 12:55 PM · inspectorhidepublic variableshow

Displaying public variables in the inspector

What I want to do: I have public variables that are showing in the inspector, but some of them aren't necessary to edit unless another variable (bool) is set to true.

Simple algorithm for what I want to do:

 public bool isActive;
 public int var1;
 public int var2;
 
 if(isActive)
 {
     //show var1 and var2 in the inspector
 }
 else
 {
    //do not show var1 and var2 in the inspector
 }

I know this isn't really a necessary feature, but it would make it look so much better.

Edit: if it's still unclear what I want to do, what I want is for variables to show up in the inspector if a bool is set to true in the inspector.

Comment
Add comment
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

2 Replies

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

Answer by Graham-Dunnett · Jul 08, 2014 at 12:56 PM

Write your own inspector, see:

http://docs.unity3d.com/ScriptReference/Editor.OnInspectorGUI.html

Comment
Add comment · 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
1

Answer by DESTRUKTORR · Jul 08, 2014 at 01:03 PM

What you probably want is either a PropertyDrawer or a custom Editor.

In either OnGUI (for a PropertyDrawer) or OnInspectorGUI (for a custom inspector Editor), you will want to use a conditional through (most likely) a toggle. There are several methods that make GUI toggles, found in GUI, GUILayout, EditorGUI, and EditorGUILayout.

Basically, contain the parts you want to have visible depending on the state of these booleans within an if statement, using said booleans as the conditions for said if statement, like so:

 if(myTarget.isActive)
 {
     // Some GUI method
 }

Within the editor or property drawer, respectively.

Comment
Add comment · 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

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

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Custom Inspector, duplicates variables ? 1 Answer

Hide GUI Text when button is pressed 1 Answer

3D model hiding and re appearing? 2 Answers

Can I hide/show objects by layer or tags? 3 Answers

Reloading a Scene throws NullReferenceException on GO with nested Prefabs 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges