Array for items (Inventory) Not Working

Array for items (Inventory) Not Working Until I Inspect the object with the script attached

Here is the script am using.

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



public class Farmer : MonoBehaviour {
	
	
[System.Serializable]
 public class Inventory {
	public string ItemName;
	public int ItemQuantity;
	public bool StackAble;
	public GameObject ItemGameObject;
 }
	private int SlotUse;
	private bool CanHit;
	private Animation EquipedAnim;

	public AllItems _AllItems;
	public Transform Grabber;
	public int GrabberDistance;

	public Item Droppeditem;
	public Texture BlankSlotImage;

	public GameObject Tool_Holder;
	public GameObject RaycastStart;
	public GameObject EquipedTool;

	public bool EquipTool;
	public bool GrabbingTool;
	public bool BuildingMode;

	public BuildingChecker _BuildingChecker;

	public Inventory[] inventory;

	RaycastHit hit;

	



	void Start () {
		Cursor.lockState = CursorLockMode.Locked;
		Cursor.visible = false;
		EquipTool = false;
		GrabbingTool = false;
		CanHit = true;
		BuildingMode = false;
		inventory = new Inventory[6];
	}
}

Error

No Error

You still need to learn a lot about object oriented programming ^^. Yes Unity can sometimes be a bit misleading because it does a lot things for you you may not be aware of. In this particular case you have marked your “Inventory” class as Serializable. Therefore Unity will serialize the class in the editor and show it inside the inspector as nested instances. The important thing here is: Who and when are all the objects we deal with created? Well your “Farmer” class instance is created when you attach it to a gameobject, either in the editor or by using AddComponent from a script.

Usually objects do not create themselfs magically. However the Unity inspector does some of that magix for you. For example your public inventory array in your Farmer class is automatically created by the inspector. When you change the size of the array in the inspector, the inspector will actually create a new array and copy the old elements over into the new array (since arrays can not be resized). Likewise because your Inventory class is serializable the inspector will create an instance of your Inspector class for every element in your array. All those created objects are serialized by the inspector and loaded again when you start your game.

However when you create objects manually, Unity will not create anything for you. This line:

inventory = new Inventory[6];

replaces the previously serialized array with a new array with 6 elements. However those 6 elements are all null. Keep in mind this is a new array with 6 elements of a reference type. So the array only contains references to Inventory instances. Though you never create any Inventory instance. You essentially have two options here:

Either create the Inventory instances inside the inspector by setting the size of the array to 6 and remove the line inventory = new Inventory[6]; from your code. That way you get the array as well as the 6 Inventory class instances automatically from the serialization system when the gameobject is deserialized.

Your other option is to manually create the object as it is common in any “normal” C# application

inventory = new Inventory[6];
for(int i = 0; i < inventory.Length; i++)
{
    inventory *= new Inventory();*

}