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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Resolved Creating class members in the inspector

Discussion in 'Scripting' started by Eidern, Jun 27, 2023.

  1. Eidern

    Eidern

    Joined:
    Mar 29, 2014
    Posts:
    94
    Hello,

    I was trying to change my code logic, and I guess I misunderstood something:

    At first I had this logic of a deck of cards which was a List of multiple cards objects like this
    Code (CSharp):
    1. [SerializeField]
    2.     List<GameObject> bornDeck;
    that I can edit in the inspector, and populate it within the inspector, it was fine.

    but Now that I need to "link" my deck to some attributes too, I think I needed to make a class
    like this one:
    Code (CSharp):
    1. public class Deck
    2. {
    3.     public string name;
    4.     public List<GameObject> cards = new List<GameObject>();
    5.     public List<int> attributes = new List<int>();
    6.  
    7. }
    but I foolishly think that it would show in the inspector when declare it like this:
    Code (CSharp):
    1. [SerializeField]
    2.     Deck tryDeck = new Deck();
    Errr... it didn't show.
    What should be the correct declaration for my usage in the inspector?
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,725
    You need to mark your class as serializable:

    Code (CSharp):
    1. [System.Serializable]
    2. public class Deck
     
    Eidern likes this.
  3. Eidern

    Eidern

    Joined:
    Mar 29, 2014
    Posts:
    94
    Oh ok I wasn't aware of that.
    thank you
     
  4. orionsyndrome

    orionsyndrome

    Joined:
    May 4, 2014
    Posts:
    3,043
    Don't get confused by doing
    new Deck()

    SerializeField doesn't allow nulls because it's a value serialization.
    An empty object will be instantiated no matter what.
    Just do
    Code (csharp):
    1. [SerializeField] Deck tryDeck;