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 Monobehavior on Prefab Loses it's Original Values When instantiated

Discussion in 'Prefabs' started by ChiliStudio, May 9, 2021.

  1. ChiliStudio

    ChiliStudio

    Joined:
    Jan 19, 2018
    Posts:
    33
    Hey guys so I have a game object which has a class that stores a struct that contains several List<Matrix4x4[]>.

    In edit mode I can initialize the values through code.

    As soon as I make it a prefab and instantiate it by dragging it into the scene from the assets folder, the values of the arrays on the monobehavior are null.

    My code setup looks like this:

    Code (CSharp):
    1. public class Tile {
    2.     public struct TileDetails{
    3.         public List<Matrix4x4[]> grassMatrices;
    4.         public List<Matrix4x4[]> rocksMatrices;
    5.     }
    6.  
    7. }
    Can anyone help me figure out why grassMatrices and rocksMatrices suddenly become null? I need to save this data on the prefab.

    Thanks,
    Aaron
     
  2. pietrodenicola

    pietrodenicola

    Unity Technologies

    Joined:
    Dec 8, 2020
    Posts:
    43
    Initializing your values within the event start() should always work.
     
  3. MartinBarrette

    MartinBarrette

    Unity Technologies

    Joined:
    Oct 22, 2019
    Posts:
    26
    Hi,
    I think the issue you are encountering is that Unity cannot serialize multidimensional array.

    Note: Unity does not support serialization of multilevel types (multidimensional arrays, jagged arrays, and nested container types). If you want to serialize these, you have two options: wrap the nested type in a class or struct, or use serialization callbacks ISerializationCallbackReceiver to perform custom serialization. For more information, see documentation on Custom Serialization.

    You can find out more information about serialization rules here: https://docs.unity3d.com/Manual/script-Serialization.html
    Regards