Search Unity

serializedObject.FindProperty( "<variablename>");

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

  1. Rowlan

    Rowlan

    Joined:
    Aug 4, 2016
    Posts:
    702
    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. xVergilx

    xVergilx

    Joined:
    Dec 22, 2014
    Posts:
    1,330
    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:
    702
    Cool, that sounds also nice, didn't know about that.