Search Unity

Organizing Variables In the Inspector

Discussion in 'Scripting' started by jimjames, Jan 5, 2014.

  1. jimjames

    jimjames

    Joined:
    Nov 23, 2013
    Posts:
    63
    I am having troubles with creating a menu in the inspector for my variables like how Vector 3 works. I have googled this and tried all the different ways of doing this and nothing works. Here is my code and im trying to make my variables organized in the inspector.

    Code (csharp):
    1.    
    2.   class Animation
    3.     {
    4.     var walkingAnimation : AnimationClip;
    5.     var sprintingAnimation : AnimationClip;
    6.     var sprintingAnimationSpeed : float = 1.5;
    7.     var jumpAnimation : AnimationClip;
    8.     var airJumpAnimation : AnimationClip;
    9.     var hoverIdleAnimation : AnimationClip;
    10.     var hoverWalkAnimation : AnimationClip;
    11.     var hoverWalkAnimationSpeed : float = 1;
    12.     var hoverSprintAnimation : AnimationClip;
    13.     var hoverSprintAnimationSpeed : float = 1;
    14.     var fallingAnimation : AnimationClip;
    15.     var idleAnimation : AnimationClip;
    16.     var blockAnimation : AnimationClip;
    17.     var airBlockAnimation : AnimationClip;
    18.     }
    19.  
    Like I said. I have googled this and tried aolt of different ways in doing this. Also used the CharacterMotor script from the assets as a refference and still could not get this to work. Please help. I am running out of hair >.<
     
  2. MeowMixEater

    MeowMixEater

    Joined:
    Dec 23, 2013
    Posts:
    18
    Not sure if I understand what you are asking but, if you are looking to make the variables visible in the inspector you need to declare them publicly.

    C#
    public float foo;

    JavaScript I think is
    public var foo : float = 0.0;

    I do now know JavaScript though.
    If this is not even close to what you are asking for my apologies.
     
  3. DexRobinson

    DexRobinson

    Joined:
    Jul 26, 2011
    Posts:
    594
  4. jimjames

    jimjames

    Joined:
    Nov 23, 2013
    Posts:
    63
    Yes it is, and i have looked at that. But i couldnt get it to work for some reason. Is "test" the script name im using it in?
     
  5. jimjames

    jimjames

    Joined:
    Nov 23, 2013
    Posts:
    63
    Got it to work. But i do not understand why i need the "var test = Test ();" at the bottom? Works without it.

    Code (csharp):
    1.  
    2. class Animations extends System.Object
    3. {
    4.     var p = 5;
    5.     var c = Color.white;
    6. }
    7.  
     
  6. DexRobinson

    DexRobinson

    Joined:
    Jul 26, 2011
    Posts:
    594
    var test = Test(); is creating an instance of test to use in your code. So in Unity's example you could call,

    Code (csharp):
    1.  
    2. class Test extends System.Object {
    3.     var p = 5;
    4.     var c = Color.white;
    5. }
    6. var test = Test ();
    7. var test2 = Test();
    8.  
    9. Debug.Log(test.p); // output's 5
    10. Debug.Log(test2.p); // output's 5
    11.  
    12. test.p = 20;
    13.  
    14. Debug.Log(test.p); // output's 20
    15. Debug.Log(test2.p); // output's 5
    16.  
     
  7. BlackMantis

    BlackMantis

    Joined:
    Feb 7, 2010
    Posts:
    1,475
    You don't need to extend an System.Object if you don't need it.

    (Tip) Don't forget that arrays can clean up variables and help reduce the var name size. You can always place the detailed names in comments.

    Code (csharp):
    1.  
    2. public var t : Test;
    3.  
    4. class Test {
    5.     //------------//
    6.     var p = 5;
    7.     //------------//
    8.     var c = Color.white;
    9.     //------------//
    10.     var animClip : AnimationClip[];
    11.     //------------//
    12. }
    13.  
     
  8. LightStriker

    LightStriker

    Joined:
    Aug 3, 2013
    Posts:
    2,717
    You're aware that if you don't declare it, it's still the same?

    Code (csharp):
    1.  
    2. public class MyClass { }
    3.  
    4. // and
    5.  
    6. public class MyClass : System.Object { }
    7.  
    8. // is exactly the same.
    9.  
    EDIT: Oups! Just noticed it's not C#. In C#, everything derive from System.Object, even if not declared as such. I have no idea about other Unity language.
     
    Last edited: Jan 8, 2014
  9. jimjames

    jimjames

    Joined:
    Nov 23, 2013
    Posts:
    63
    DexRobinson

    Sorry but what is the point in making a class a variable? What can I do with it as a variable?
     
  10. ThermalFusion

    ThermalFusion

    Joined:
    May 1, 2011
    Posts:
    906
    It is, for example, an easy way of organizing lots of related variables in the inspector without having to write a custom editor.