์์๋ณด๊ณ ์ธ๋ฒคํ ๋ฆฌ ๊ตฌํ์ค์ธ๋ฐ,
Item ๋ฐ์ดํฐ๋ฅผ scriptableObject๋ก ๋ง๋ค๊ณ ์์ ๋ฉ๋ด์ ์ถ๊ฐํด์
public Item[] Items;์ hierarchey์์ item ์์ ๋ฉ๋ด ๋ฐ์ดํฐ๋ค ์ถ๊ฐํ๊ณ
๋ค๋ฅธ ๋ฉ์๋์์ Items[int ~~]๋ก ์ฐธ์กฐํ๋ ค๊ณ ํ๋๊น nullreference ์ค๋ฅ ๋จ๋๋ฐ ์์ด๋ฌ๋์ง ๋ํต ๋ชจ๋ฅด๊ฒ ์..
๋ช ์๊ฐ์งธ ํค๋งค๊ณ ์๋๋ฐ ์ํด๋ ์ข์ผ๋ ์๋ ค์ค
[CreateAssetMenu(menuName = "Scriptable object/Item")]
public class Item : ScriptableObject
{
[Header("Only gameplay")]
public ItemType type;
public ActionType actionType;
[Header("Only UI")]
public bool stackable = true;
[Header("Both")]
public Sprite image;
}
public enum ItemType
{
Quest,
posion,
}
public enum ActionType
{
Healing,
speedUp,
required_in_Quest2
}
>>> ์ด๋ ๊ฒ ๋ง๋ค์ด์ง ๋ฉ๋ด๋ฅผ ์์ ์ฌ์ง์ฒ๋ผ ๋ฑ๋กํ๊ณ
//
public void GetItem(int id)
{
inventoryManager.AddItem(fieldItems[id]);
}
์ด๋ ๊ฒ ์ฐธ์กฐ ๋ฉ์๋ ํธ์ถํ๋ฉด์ ์ด์ด๊ฐ๋๊ฑด๋ฐ ๋ค๋ฅธ ๋ฉ์๋๋ก ๋์ด๊ฐ๊ธฐ ์ ์ GetItem์์๋ถํฐ nullexep
NullReferenceException ์ค๋ฅ๊ฐ ๋ธ... ๋ฉ๋ด์์ image๋ ๋ค ํ ๋นํด๋จ๋๋ฐ ๋ญ๊ฐ ๋ฌธ์ ์ผ๊น...
---------------------------------------------------------------------------------------------------------------------
(์ถ๊ฐ)
public class InventoryManager : MonoBehaviour
{
public int maxStackedItems = 4;
public Inventory_Slot[] inventory_slots;
public GameObject inventoryItemPrefab;
public void AddItem(Item item)
{
// ์์ดํ ์คํ ๊ฒ์ฌ
for (int i = 0; i < inventory_slots.Length; i++)
{
Inventory_Slot slot = inventory_slots[i];
Inventory_Item itemInSlot = slot.GetComponentInChildren<Inventory_Item>();
if (itemInSlot == null &&
itemInSlot.item == item &&
itemInSlot.count < maxStackedItems)
{
itemInSlot.count++;
itemInSlot.RefreshCount();
return true;
}
}
// ๋น ์ฌ๋กฏ ์ฐพ๊ธฐ
for (int i = 0; i < inventory_slots.Length; i++)
{
Inventory_Slot slot = inventory_slots[i];
Inventory_Item itemInSlot = slot.GetComponentInChildren<Inventory_Item>();
if (itemInSlot == null)
{
SpawnNewItem(item, slot);
return true;
}
}
return false;
}
void SpawnNewItem(Item item, Inventory_Slot slot)
{
GameObject newItemGo = Instantiate(inventoryItemPrefab, slot.transform);
Inventory_Item inventory_Item = newItemGo.GetComponent<Inventory_Item>();
inventory_Item.InitialiseItem(item);
}
์ฝ๋๋ฅผ ๋ณด๋ฉด ๋ฌธ์ ๋ฅผ ๋นจ๋ฆฌ ์ฐพ์ ์ ์์ด์
์ฝ๋ ์ฌ๋ ธ์ต๋๋ค
์ด์ํ๊ตฐ์... ์ธ๋ฒคํ ๋ฆฌ ๋งค๋์ ์ชฝ ์ฝ๋๋ ๋ณผ์์์๊น์
์ถ๊ฐํ์ต๋๋ค
์์ดํ ์คํ๊ฒ์ฌ ์ด์ชฝ์ if (itemInSlot == null && itemInSlot.item == item && itemInSlot.count < maxStackedItems) ์ด๋ถ๋ถ์ด์, itemInSlot != null์ด ๋ง์๋ฏ ์ถ๋ค์
์.. ํด๊ฒฐ๋์ต๋๋ค ๊ฐ์ฌํฉ๋๋ค ๊ฐ์ฌํฉ๋๋ค ๊ฐ์ฌํฉ๋๋ค ๊ฐ์ฌํฉ๋๋ค