Hello, I'm new to coding and I found code for inventory and there i an error that I've looked for an answer to but I can't find an answer for.
This is the Inventory code:
----------
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Inventory : MonoBehaviour {
public Image[] itemImages = new Image[numItemsSlots];
private static readonly SpriteMeshType[] SpriteMeshType = new SpriteMeshType[numItemsSlots];
private ContextMenuItemAttribute[] items;
public const int numItemsSlots = 4;
public ContextMenuItemAttribute[] Items
{
get
{
return items;
}
set
{
items = value;
}
}
public void AddItem(ContextMenuItemAttribute itemToAdd)
{
for (int i = 0; i < Items.Length; i++)
{
if (Items[i] == null)
{
Items[i] = itemToAdd;
itemImages[i].enabled = true;
return;
}
}
}
public void RemoveItem (ContextMenuItemAttribute itemToRemove)
{
for (int i = 0; i < Items.Length; i++)
{
if (Items[i] == itemToRemove)
{
Items[i] = null;
itemImages[i].sprite = null;
itemImages[i].enabled = false;
return;
}
}
}
public override bool Equals(object obj)
{
var inventory = obj as Inventory;
return inventory != null &&
base.Equals(obj) &&
EqualityComparer.Default.Equals(items, inventory.items);
}
public override int GetHashCode()
{
return base.GetHashCode();
}
public override string ToString()
{
return base.ToString();
}
}
----------
There isn't anything wrong there (I believe) but I don't know if you will need it or not so... on the safe side.
But the Inventory Editor has 4 errors (and 6 warning apparently), and here it is:
----------
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
[CustomEditor(typeof(Inventory))]
public class InventoryEditor : Editor {
private readonly bool[] showItemSlots = new bool[Inventory.numItemsSlots];
private SerializedProperty itemsImagesProperty;
private SerializedProperty itemsProperty;
private const string InventoryPropertyItemsImagesName = "itemImages";
private const string InventoryPropertyItemsName = "items";
public static string InventoryPropertyItemsName1
{
get
{
return InventoryPropertyItemsName;
}
}
private void OnEnable()
{
itemsImagesProperty = SerializedObject.FindProperty(InventoryPropertyItemsImagesName);
itemsProperty = SerializedObject.FindProperty(propertyPath: InventoryPropertyItemsImagesName)
;
}
public override void OnInspectorGUI()
{
SerializedObject.Update();
for (int i = 0; i < Inventory.numItemsSlots; i++)
{
ItemSlotGUI(i);
}
SerializedObject.ApplyModifiedProperties();
}
private void ItemSlotGUI (int index)
{
EditorGUILayout.BeginVertical(GUI.skin.box);
EditorGUI.indentLevel++;
showItemSlots[index] = EditorGUILayout.Foldout (showItemSlots[index], "Item slot" + index);
if (showItemSlots[index])
{
EditorGUILayout.PropertyField (itemsImagesProperty.GetArrayElementAtIndex(index));
EditorGUILayout.PropertyField (itemsProperty.GetArrayElementAtIndex(index));
}
EditorGUI.indentLevel--;
EditorGUILayout.EndVertical();
}
}
----------
The errors are all in the areas the **SerializedObject** and I don't know how to fix the errors, so help would be really awesome. Please and thank you.,I found a code written for inventory and I copied it down, with it there was Inventory editor and there was a problem. **Error code CS0120, it says, "An object reference is required for the non-static field, method, or property..."**
*Thank you to the person who helped me clean this question/ask/request up, the tips help a lot to make this easier for others.*
↧