Search Unity

Question Inspector doesn't display my struct

Discussion in 'Editor & General Support' started by heeyoung_abc, Feb 15, 2024.

  1. heeyoung_abc

    heeyoung_abc

    Joined:
    Jun 11, 2023
    Posts:
    12
    This is my code:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Test : MonoBehaviour
    6. {
    7.     // Start is called before the first frame update
    8.     [System.Serializable]
    9.     public struct TestField
    10.     {
    11.         int a, b;
    12.     }
    13.     [SerializeField] TestField t;
    14.     void Start()
    15.     {
    16.        
    17.     }
    18.  
    19.     // Update is called once per frame
    20.     void Update()
    21.     {
    22.        
    23.     }
    24. }
    25.  
    upload_2024-2-15_21-43-23.png
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,705
    In C#
    struct
    members are
    private
    by default.

    Make them
    public
    if you want to see them in Unity, or else use a
    [SerializeField]
    decorator.

    Before you venture too much deeper into struct usage, be sure you understand the distinction between Value Types and Reference Types or you are going to observe a lot of really baffling behavior.
     
  3. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,909
    To expand on what Kurt mentioned above, you should review the Unity Serialization Rules to understand what you need to get things to serialize in Unity.