Search Unity

Why are some instance properties drawn in the inspector when MonoBehaviour file is selected?

Discussion in 'Editor & General Support' started by hedgehog90, Oct 11, 2016.

  1. hedgehog90

    hedgehog90

    Joined:
    May 18, 2016
    Posts:
    27
    Just curious, wondering if this is a bug or not.
    If I select a MonoBehaviour .cs file (ie - not the component, the actual script file in the project pane), properties that are derived from UnityEngine.Object are drawn in the inspector like so:

    2016-10-11 15_09_41-Program Manager.png

    (I have the inspector in Debug mode, but the inspector draws the same in Normal mode too)

    What is the purpose of this? Nothing will happen if I set that property in the inspector to all the instances of said Monobehaviour. Weird.
     
  2. neginfinity

    neginfinity

    Joined:
    Jan 27, 2013
    Posts:
    13,573
    IIRC monobehaviour SCRIPT can have its own properties. The purpose of this is providing something like configurable defaults or global variables.

    For example if your script needs to load a shader, and you want to specify where that shader is without hardcoding, this can be done via script-level properties.

    I haven't used this much much.

    Also... you're looking at the script in debug mode.
     
    angrypenguin likes this.
  3. ScottyB

    ScottyB

    Joined:
    Apr 4, 2011
    Posts:
    146
    Yes, neginfinity is correct, it is for adding default variable assignments. If you have one of those variables assigned, the next time you add that MonoBehaviour to a GameObject that variable will be already assigned. Also note that changing that variable assignment will not update any GameObjects with that MonoBehaviour already added as overwriting any values you had already assigned would be super annoying.
     
    angrypenguin likes this.
  4. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    15,620
    Yes, a script is just like any other type of "asset" in that it can have some data associated with it at the project level.
     
  5. hedgehog90

    hedgehog90

    Joined:
    May 18, 2016
    Posts:
    27
    Oh right. So how do I tell Unity that these aren't global properties? Is there an attribute I can add to the field/class?
    It just assumes for some reason that if there's an Object field that it's global.
     
  6. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Just leave it blank.
     
  7. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    15,620
    They aren't "global properties". They just set a default value that's assigned to newly created instances.