Search Unity

  1. Improved Prefab workflow (includes Nested Prefabs!), 2D isometric Tilemap and more! Get the 2018.3 Beta now.
    Dismiss Notice
  2. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  3. Let us know a bit about your interests, and if you'd like to become more directly involved. Take our survey!
    Dismiss Notice
  4. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice
  5. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice

serializedObject.FindProperty( "<variablename>");

Discussion in 'Editor & General Support' started by Rowlan, Oct 10, 2018.

  1. Rowlan

    Rowlan

    Joined:
    Aug 4, 2016
    Posts:
    508
    I just wanted to toy around with the editor and stumbled upon this:

    Code (CSharp):
    1. serializedObject.FindProperty( "<variablename>");
    What's the reason for this? I pretty much avoid all kinds of string literals because all they give you is runtime problems when you change your code. Previously I made constants for that kind of references in order to be able to change them at 1 place.

    I stumbled upon the BaseEditor.cs class from the PostProcessing stack and it solves exactly that problem by allowing you to use the variables themselves instead of names.

    I've been wondering about that for a long time now in regards to Unity. Is there a specific reason for the usage of strings as identifiers which can only cause runtime exceptions?
     
  2. VergilUa

    VergilUa

    Joined:
    Dec 22, 2014
    Posts:
    740
    Yeah, it's ugly. If you're using .Net 4.x, you could try to use nameof(variable) instead of string.
     
    Rowlan likes this.
  3. Rowlan

    Rowlan

    Joined:
    Aug 4, 2016
    Posts:
    508
    Cool, that sounds also nice, didn't know about that.