Search Unity

A Question About Special or Reserved Variable Names

Discussion in 'Scripting' started by liquidgraph, Jun 25, 2011.

  1. liquidgraph

    liquidgraph

    Joined:
    Feb 26, 2009
    Posts:
    324
    I've always been puzzled when coding in Unitron and coming upon special variables that get colored orange. I'm talking about variable names like "player", "GUI", "button", "axis", "main", "asset", "audio", etc.

    In general, if I come across an orange name, I never use it as a custom variable because I'm afraid it will override, or be overridden by, something else. But this can become a bother. I like to keep my naming conventions consistent. For example, I have a "Player" class and I want to use the "player" variable to store an instance of it, but "player" is one of these special orange names. I don't want to call the instance something awkward like "player_instance". To me, the lower-case "p" indicated that it's an instance already, but in this case "player" is one of those special names. Is it safe to use? How do we know which names are safe to use and which ones aren't? I'm talking about C# specifically, if that makes a difference.
     
  2. Dreamblur

    Dreamblur

    Joined:
    Jun 18, 2011
    Posts:
    183
    Here.

    If you're afraid of overriding an inherited variable from a parent class, then here's what I think of that: if you didn't know about it, then it's prolly not that important. XD (Obviously joking, but it applies to a lot of classes.)
     
  3. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    The only thing you can't use for variable names are the language reserved words, such as int, float, etc. You can even call a variable something like "transform", though if you want to refer to the object's Transform after doing that, you'd have to use this.transform.position instead (or use GetComponent). So that's more confusing than it's worth, but other than that, and avoiding reserved words, use whatever makes the most sense.

    --Eric
     
  4. liquidgraph

    liquidgraph

    Joined:
    Feb 26, 2009
    Posts:
    324
    That's just the thing, I need a list of Unity-specific reserved words. For example, I'm still not clear, is "player" a reserved keyword? How about "button"? How about "main"? How about "type"?

    I want to be able to say, for example: Player.Type = something. But for this to work in my scheme, "player" and "type" (the lower-case versions) must be usable as well. I know the upper case versions are fine.
     
    Last edited: Jun 25, 2011
  5. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    There aren't any that I know of.

    Code (csharp):
    1. var Vector3 = 5;
    2. var Start = 100;
    3. var print = 50;
    4. print (Vector3 + " " + Start + " " + print); // works fine
    If you get a compiler error, you can't use it. Otherwise, you can.

    --Eric
     
  6. Dreamblur

    Dreamblur

    Joined:
    Jun 18, 2011
    Posts:
    183
    If you're working on Unity's own script editor, any reserved keywords are bolded, and using them in a context where you're not supposed to will generate an error. Some variables in actual classes are colored, but those are only hints that they are variables in some class, not necessarily the class that you're inheriting from. If you want to be absolutely sure that you're not overriding an inherited variable, then all you have to do is check the documentation for the parent class in the scripting reference. There are no Unity-specific reserved words. Only programming languages have reserved words.
     
  7. liquidgraph

    liquidgraph

    Joined:
    Feb 26, 2009
    Posts:
    324
    Interesting. I never knew something like "int Vector3 = 5;" was legal.