Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

How can i hide variables from the inspector ?

Discussion in 'Scripting' started by benydayag, Aug 23, 2016.

  1. benydayag

    benydayag

    Joined:
    Aug 1, 2016
    Posts:
    91
    Should i use [HideInInspector] or @HideInInspector ?

    And how do i make it for a single var or many vars in a block ?
    For example from a single var:

    Code (csharp):
    1. [HideInInspector]publicfloat x =3.0f;
    But if i have many vars like:

    Code (csharp):
    1. int x = 0; int y = 0; int z = 0;
    How do i put all of them togeather under a block to be hided from the inspector ?
     
  2. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    The first one for C# the second one for JS.

    You can't - each one has to be decorated individually.
     
    benydayag and LeftyRighty like this.
  3. zaxvax

    zaxvax

    Joined:
    Jun 9, 2012
    Posts:
    220
    However you can improve it a lil bit in terms of readability.

    Code (CSharp):
    1.     [HideInInspector] public float f1, f2, f3, f4 = 0f;
    2.     [HideInInspector] public float f5 = 2f, f6 = 1f;
     
  4. pezz

    pezz

    Joined:
    Apr 29, 2011
    Posts:
    606
    declare variable as private.
     
  5. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    whilst that does remove things from the inspector it's not always an option, i.e. if you're using editor scripts you need to keep the affected variables as public for them to be altered by the editor script but you might not want the game designers or whoever is using the component to manipulate those values directly.
     
  6. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    does anyone know if there is a way to alter visual studio to follow this formatting? it insists on the var being below the decoration :mad:
     
  7. passerbycmc

    passerbycmc

    Joined:
    Feb 12, 2015
    Posts:
    1,738
    out of the box now, but ReSharper lets you customize that setting,and i would have to assume some other extensions do as well
     
  8. ShadowOrigin288

    ShadowOrigin288

    Joined:
    Aug 31, 2021
    Posts:
    2
    For that u can also use This method:

    Code (CSharp):
    1. public class h : monobehavior {
    2.         [Serializable]
    3.         public class ff
    4.         {
    5.             public bool g;
    6.         }
    7.         public ff f;
    8. }

    Or

    Code (CSharp):
    1. [Serializable]
    2. public class ff
    3. {
    4.     public bool g;
    5. }
    6.  
    7. public class h : monobehavior {
    8.         public ff f;
    9. }

    You can add all the variables you want to hide in ff class that you want to hide. You can add more class like ff in a single script or create other script for it. Just remember to remove monobehavior and add [Serializable] above the class