C# Custom Class - Declare Variable's Value

Ok, I made a custom Item class that does not inherit from MonoBehavior:

using UnityEngine;
using System.Collections;

public class Item {

	public string id;
	public string displayName;
	public GameObject itemPrefab;
	public Texture2D displayImage;
	public string description;
	public int maxStack;
	public int currentStack = 1;

Notice, I set currentStack to = 1. Below is my ItemList Script. It just declares a list of the items that can be in the game.

using UnityEngine;
using System.Collections;

public class ItemList : MonoBehaviour 
	public Item[] items;

I then can put the Item’s attributes in the array in the Inspector.

The problem is this: currentStack is set to 0. I’ll take off the HideInInspector to check it, and it says 0, not 1. How do I set currentStack to always start off as 1?

You can do that by actually initializing the array. Then it creates the references as elements in that array.

int amountOfAllItems;
Item[] items = new Item[amountOfAllItems];

May not work %100 but that’s what it think it is.

By the way you don’t need to use HideInInspector property because you can’t attach this script to any gameobjects since it’s not a monobehaviour so it will never be seen in the inspector anyway.

Plus you can’t serialize that class if you intend to use .net serialization because types of GameObject, Texture2D and others that are from UnityEngine are not marked as [Serializable] like your class is right now hence it will give some serialization exception.