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

Private Variables not Saving with Prefab.

Discussion in 'Scripting' started by prattmyster, Jan 6, 2022.

  1. prattmyster

    prattmyster

    Joined:
    Apr 15, 2015
    Posts:
    17
    I have created an asset creation system in editor. The base class my Asset inherits from has some private/readonly variables that i can set when the function is called, but doesn't save in the gameobject it self.

    I.E

    public class Base_Asset : MonoBehaviour
    {
    private string filename;
    private string asset_name;
    private string description;
    private Item_Type asset_type;
    private Material_Types shader;
    private Texture2D icon; // Pic used for Tooltip.
    private Texture2D thumbnail; // Pic used for Toolbar.
    private TextureSets textures;
    private TextureSets lodtextures;
    private readonly bool vanilla;
    private Mesh base_mesh;
    private Mesh base_mesh_lod;

    public void Create_Base_Asseet(Base_Asset_Information info)
    {
    filename = info.Filename;
    asset_name = info.Asset_Name;
    description = info.Description;
    asset_type = info.Type;
    shader = info.Shader;
    icon = info.Icon;
    thumbnail = info.Thumbnail;
    textures = info.Textures;
    lodtextures = info.LODTextures;
    base_mesh = info.Base_Mesh;
    base_mesh_lod = info.Base_Mesh_lod;

    new Base_Asset(false);
    #if (UNITY_EDITOR)
    new Base_Asset(true);
    #endif
    }

    public Base_Asset()
    {

    }

    Base_Asset(bool value)
    {

    vanilla = value;
    Debug.Log(vanilla.ToString());
    }

    public bool isVanilla()
    {
    return vanilla;
    }

    this is the Base Script my Asset inherits from. The debug.Log(vanilla.ToString()) gives back the right result but when i call isVanilla() from the gameobject it returns another value.

    quite new to scripting so any help would be apreciated
     
    Last edited: Jan 6, 2022
  2. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    Private variables aren't serialized by Unity unless they're marked with [SerializeField]. You also shouldn't be creating MonoBehaviours with the new keyword, nor should you have a constructor.
     
    prattmyster likes this.
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,749
    In the future, if you post a code snippet, ALWAYS USE CODE TAGS:

    How to use code tags: https://forum.unity.com/threads/using-code-tags-properly.143875/

    Unity does not serialize private variables unless they are decorated with the SerializeField attribute.

    Also, you can NEVER use the new operator to make classes that derive from UnityEngine.Object classes.

    This is forbidden:

    Before you go too far further off the rails with this, I suggest starting with some basic coding tutorials in Unity. You need to behave accordingly otherwise almost nothing will function and everything will seem mysterious to you.

    Imphenzia / imphenzia - super-basic Unity tutorial:



    Jason Weimann:



    Brackeys super-basic Unity Tutorial series:



    Sebastian Lague Intro to Game Development with Unity and C#:

     
    prattmyster likes this.
  4. prattmyster

    prattmyster

    Joined:
    Apr 15, 2015
    Posts:
    17
    Sorted now.
    i was setting the variables in that class not the game objects class so just feed the gameobject through the function and is all working fine now. :)