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

Feature Request [ANSWERED] Constant vector shorthands

Discussion in 'Scripting' started by Deleted User, Apr 19, 2022.

  1. Deleted User

    Deleted User

    Guest

    Edit: I am stupid.

    A simple suggestion: When using Vector shorthands, like
    Vector2Int.left
    , they're accessed as properties and aren't declared as constants. I can't think of a good reason for them to be this way (their definition is to be shorthands for something like
    new Vector2Int(-1, 0)
    , so they won't change at runtime. However, them being implemented this way makes them impossible to use as a case match in a switch statement: upload_2022-4-19_10-33-55.png
    While not a big deal, it's annoying having to use workarounds when checking for vector equality in switch statements.
     
    Last edited by a moderator: Apr 19, 2022
  2. nijnstein

    nijnstein

    Joined:
    Feb 6, 2021
    Posts:
    78
    edit: sorry i really thought vector2int was a class in unity..

    you cannot have a constant class, as Vector2Int is a class IT ISNT it just wont work per language spec:

    "Constants are immutable values which are known at compile time and do not change for the life of the program. Constants are declared with the const modifier. Only the C# built-in types (excluding System.Object) may be declared as const. User-defined types, including classes, structs, and arrays, cannot be const. Use the readonly modifier to create a class, struct, or array that is initialized one time at run time (for example in a constructor) and thereafter cannot be changed."
    https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/constants
     
  3. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,842
    They're structs. They can't be constant.
     
    Deleted User, Bunny83 and Dextozz like this.
  4. Deleted User

    Deleted User

    Guest

    You're absolutely correct. I completely overlooked that after doing too much vanilla C# programming. I will mark this one as solved.