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

Question Issue with get field from SerializedProperty

Discussion in 'Editor & General Support' started by ElectrumGames, May 5, 2023.

  1. ElectrumGames

    ElectrumGames

    Joined:
    Jul 9, 2022
    Posts:
    11
    Hello, I have an issue with get field from SerializedProperty when I try to make Editor script.
    I use Unity 2020.3.45f1
    When I try to get any field from my SO(this also added in inspector) - i always get Null. For example - I make public int field and try to get it, but I have Null at console. Code looks like this:
    Code (CSharp):
    1. var levelsProperty = serializedObject.FindProperty("levelsInfo").FindPropertyRelative("a");
    2.            
    3. Debug.Log(levelsProperty); //<- here console write Null
    Here full code:
    Editor script: https://pastebin.com/JhGgAcVm
    Level creator: https://pastebin.com/LMqG0WyD
    Levels Info SO: https://pastebin.com/Zxhz7iuF
    And, maybe, this is important - I try to get LevelData array to handle it in my Editor Script. LevelData: https://pastebin.com/zcYU0Lmi

    Maybe someone know how to solve this issue?
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,749
    The code above prevents you from even reasoning about what is null. Fix that first.

    If you have more than one or two dots (.) in a single statement, you're just being mean to yourself.

    How to break down hairy lines of code:

    http://plbm.com/?p=248

    Break it up, practice social distancing in your code, one thing per line please.

    "Programming is hard enough without making it harder for ourselves." - angrypenguin on Unity3D forums

    "Combining a bunch of stuff into one line always feels satisfying, but it's always a PITA to debug." - StarManta on the Unity3D forums

    After you fix the above tangled mess:

    How to fix a NullReferenceException error

    https://forum.unity.com/threads/how-to-fix-a-nullreferenceexception-error.1230297/

    Three steps to success:
    - Identify what is null <-- any other action taken before this step is WASTED TIME
    - Identify why it is null
    - Fix that
     
  3. ElectrumGames

    ElectrumGames

    Joined:
    Jul 9, 2022
    Posts:
    11
    About code at top: I write this block for skip answer "How I get SerializedProperty", so if you open editor script paste - you can see code:
    Code (CSharp):
    1. var levelsProperty = LevelsInfoProperty.FindPropertyRelative("a");
    So it don't have more than one dot in expression.

    What I know about issue: LevelsInfoProperty - isn't null, and I can change it at other code segment, than apply changes(look at Editor script). If I use debug mode in Rider - IDE shows my data at property:

    BUT idk why method FindPropertyRelative can't find any from my properties in object with [Serializable] and in attempts to solve issue - I make all fields to public
     
    Last edited: May 5, 2023