์˜์ƒ๋ณด๊ณ  ์ธ๋ฒคํ† ๋ฆฌ ๊ตฌํ˜„์ค‘์ธ๋ฐ,

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

}




a16711aa372a6ae87eb1d19528d527034d7b3b3db879



>>> ์ด๋ ‡๊ฒŒ ๋งŒ๋“ค์–ด์ง„ ๋ฉ”๋‰ด๋ฅผ ์œ„์˜ ์‚ฌ์ง„์ฒ˜๋Ÿผ ๋“ฑ๋กํ•˜๊ณ 


//

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);

}