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

Feedback Inspector property values converted to constants at compile time

Discussion in 'Editor & General Support' started by Ziplock9000, Dec 18, 2020.

  1. Ziplock9000

    Ziplock9000

    Joined:
    Jan 26, 2016
    Posts:
    360
    When we expose a gameobject's script's properties and variables to an inspector we can change those values at design time and runtime because they are essentially variables.

    However, the majority of these values in the inspector we realistically only change at design time. So it would make sense for the compiler to convert these none changing values to constants behind the scenes so they would perform faster than properties/fields. Potentially this would happen when a property/variable is marked with something like [RuntimeStatic] or something?

    In a way, it's similar to marking a gameobject as static.

    I wonder if the Unity team would do something like this?
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,697
    I don't think it's really possible to make them constants for a few reasons:

    - they are instance values not static values. (They belong to a particular instance of the class, not the class as a whole)
    - there's nothing stopping you from changing the values at runtime, and even with some static analysis, Unity couldn't detect reflective access.
     
  3. Ziplock9000

    Ziplock9000

    Joined:
    Jan 26, 2016
    Posts:
    360
    Good points. Because constants are static by nature, they share the same value across class instances. So putting the same script on multiple objects would not work when the values you use in the inspector are different.

    It seems logical in one way, but not in another. Due to class instances being instantiated at runtime, not compile-time this doesn't make sense in a C# OO world but does in a more general notion of constant values that are defined BEFORE compilation.
     
    PraetorBlue likes this.