Search Unity

Question Why would one of my Serialized custom classes display in inspector but another won't?

Discussion in 'Scripting' started by jimpbblmk, May 18, 2023.

  1. jimpbblmk

    jimpbblmk

    Joined:
    Jan 14, 2014
    Posts:
    1
    I wrote several relatively simple custom classes for my sample project (a clone of the Ticket to Ride board game) and marked them all Serializable to check their values as I go. The first few classes displayed their values just fine, but the next batch of classes did not.

    The Board class displays contents of the City (not uploaded), TrackPath, (not uploaded) TrackSinglePath (not uploaded), and TrackBrick classes just fine without writing any custom editor code or anything, but then TrainCardDeck, comprised of TrainCard classes isn't displaying the same way.

    I'm not sure what the difference in what I did is, especially if the solution ends up being that I need to customize the editor, because it's weird that I should have to do that for some of my classes but not all, right?

    [If you need to see the three classes I didn't upload, let me know. I could only upload 5 attachments here.]

    Some of the calling code in my main game script:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class GameMain : MonoBehaviour
    6. {
    7.     ...
    8.     TrackBrick[] usaSinglePathAtlantaCharleston, usaSinglePathAtlantaMiami, usaSinglePathAtlantaNashville, ... ;
    9.     TrackSinglePath[] usaPathAtlantaCharleston, usaPathAtlantaMiami, usaPathAtlantaNashville, ... ;
    10.     TrackPath[] usaPaths;
    11.     ...
    12.     [SerializeField]
    13.     public TrainCardDeck deckTrainCards;
    14.  
    15.     public float BOARD_USA_WIDTH = 935f, BOARD_USA_HEIGHT = 560f;
    16.     public Board usa;
    17.  
    18.     void Start()
    19.     {
    20.         mapSR = GetComponent<SpriteRenderer>();
    21.  
    22.         mapObjectsParent = GameObject.FindGameObjectWithTag("MapObjects").transform;
    23.  
    24.         usaCities = new City[] { new City("Atlanta", 745, 380),
    25.                                  new City("Boston", 910, 95), ... };
    26.  
    27.         #region TrackBrick[] CONSTRUCTORS
    28.  
    29.         usaSinglePathAtlantaCharleston = new TrackBrick[] { new TrackBrick(775, 390, 90.5f, TrackColor.any),
    30.                                                             new TrackBrick(810, 391, 90.5f, TrackColor.any) };
    31.         usaSinglePathAtlantaMiami = new TrackBrick[] { new TrackBrick(759, 409, -40.8f, TrackColor.blue),
    32.                                                        new TrackBrick(781, 437, -38.9f, TrackColor.blue),
    33.                                                        new TrackBrick(803, 464, -39.4f, TrackColor.blue),
    34.                                                        new TrackBrick(825, 491, -39.5f, TrackColor.blue),
    35.                                                        new TrackBrick(847, 518, -39f, TrackColor.blue) };
    36.         ...
    37.  
    38.         #endregion
    39.  
    40.         #region TrackSinglePath[] CONSTRUCTORS
    41.  
    42.         usaPathAtlantaCharleston = new TrackSinglePath[] { new TrackSinglePath(usaSinglePathAtlantaCharleston) };
    43.         usaPathAtlantaMiami = new TrackSinglePath[] { new TrackSinglePath(usaSinglePathAtlantaMiami) };
    44.         ...
    45.  
    46.         #endregion
    47.  
    48.         usaPaths = new TrackPath[] { new TrackPath("Atlanta", "Charleston", true, usaPathAtlantaCharleston),
    49.                                      new TrackPath("Atlanta", "Miami", true, usaPathAtlantaMiami), ... };
    50.  
    51.         #region Card AND Deck OBJECT CONSTRUCTORS
    52.  
    53.         deckTrainCards = new TrainCardDeck();
    54.         //add USA base game train cards
    55.         deckTrainCards.AddCard(new TrainCard("T01001", TrackColor.black, texture_TrainCardBlack));
    56.         deckTrainCards.AddCard(new TrainCard("T01002", TrackColor.black, texture_TrainCardBlack));
    57.         deckTrainCards.AddCard(new TrainCard("T01003", TrackColor.black, texture_TrainCardBlack));
    58.         ...
    59.         deckTrainCards.AddCard(new TrainCard("T01110", TrackColor.black, texture_TrainCardLoco));
    60.      
    61.         #endregion
    62.  
    63.         usa = new Board("USA", BOARD_USA_WIDTH, BOARD_USA_HEIGHT, usaCities, usaPaths);
    64.  
    65.         PlaceCitiesOnMap(usa);
    66.         PlaceEmptyTrackPathsOnMap(usa);
    67.     }
    68.  
    69.     void Update()
    70.     {
    71.      
    72.     }
    73.  
    74.     ...
    75.  
    76. }
     

    Attached Files:

  2. Adrian

    Adrian

    Joined:
    Apr 5, 2008
    Posts:
    1,066
    Your
    TrainCardDeck
    uses
    LinkedList<TrainCard>
    , which is not supported by Unity. Only
    List<TrainCard>
    or
    TrainCard[]
    are.

    (I assume that's just a mixup but just clarify, there's almost never a reason to use linked lists nowadays. They were good decades ago when memory was relatively fast compared to CPU speeds but nowadays memory access is way too slow to make linked lists worthwhile.)

    Also, there are many
    SerializeField
    attributes on methods, which do nothing. They only have an effect on private fields, I would remove them from all methods.
     
    Bunny83 and spiney199 like this.