Using inspector to assigning values to the fields for other classes.

Hi there! I’m creating a simple clicker game and now trying to use OOP to deal with its Items Shop. Because all the items will have the same fields I decided to create the base class - ItemShop.

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public abstract class ShopItem : MonoBehaviour
{
    public string UpgradeName;
    public int InitialCost;
    public Button MyButton;
    public Text MyButtonText;
    public int LevelOfItem { get; protected set; }
    public abstract int CurrentCost { get; }
    ...
}

Then I created the second one, DefaultUpgradeItem class, which inherits from it and also have some public fields.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class DefaultUpgradeItem : ShopItem
{
  public int SomeField;
  ...
}

and declare this derived class as a public field to the Main Game script.

public class Game : MonoBehaviour
{
    public DefaultUpgradeItem testDefaultItem;
    ...
}

But when I’d attached the Game script to a GameObject on my scene I noticed that I can assign any fields to the Both ShopItem and DefaultUpgradeItem classes from the inspector. Is there any way to do it in the Unity inspector or it’s possible only in code?

Are you asking if you can hide the inspector fields so that you can only assign these values in code? If so, yes, simply add “[HideInInspector]” before the keyword “public” for each field you want hidden in the inspector.

Basically, any field that is a serialized field will appear in the inspector. Public fields are serialized by default, private fields can be serialized if you add [SerializeField] before them.

1 Like

Thank you for your answer