Morning, I’m developing an android top down shooter kind of style game and just wanted some pointers regarding an equip system. Currently, I have a beta inventory system that adds items from a database (item database is a list of items). The system as it stands literally just populates a scroll rect with items on the click of a button.
What I would like help with is actually telling my character which weapons it has equipped. On my equipment screen I have 3 weapon slots, and when either of them is pressed it opens up the inventory window (I’d thought of assigning a value for slotID when the inventory window is opened so that when ‘Equip’ is pressed it knows which slot to equip to). Could anyone help me implement this?
Once that is done my plan was to just create another list of EquippedItems, and use Don’tDestroyOnLoad (), and when the game starts I could instantiate the equipped items simultaneously. I’m worried that I’m doing a lot of work in vain at the moment and not really sure if what I want to do is possible using this list as an item database.
Here is what I’m using at the moment:
Item
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[System.Serializable]
public class Item {
public string itemName;
public string itemDescription;
public int itemID;
public Sprite itemSprite;
//public string itemSpriteName;
public GameObject itemPrefab;
//public string itemPrefabName;
public Item (string name, string description, int id, string prefabName)
{
itemName = name;
itemDescription = description;
itemID = id;
}
public Item ()
{
}
}
Item Database
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ItemDatabase : MonoBehaviour {
public List items = new List();
private void Awake ()
{
DontDestroyOnLoad (this.gameObject);
}
public Item GetItemByID (int id)
{
foreach (Item itm in items) {
if (itm.itemID == id) {
return itm;
}
}
Debug.Log (“Can’t find item by ID!”);
return null;
}
}
Inventory
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Inventory : MonoBehaviour {
public int slotsAmount;
public GameObject slotPrefab;
public GameObject itemPrefab;
public GameObject equipButtonPrefab;
public GameObject slotParent;
public List slots = new List();
private ItemDatabase itemDatabase;
private void Awake ()
{
DontDestroyOnLoad (this.gameObject);
}
private void Start ()
{
itemDatabase = GameObject.FindGameObjectWithTag (“ItemDatabase”).GetComponent ();
}
public void CreateSlot ()
{
slots.Add(Instantiate (slotPrefab));
for (int i = 0; i < slots.Count; i++)
{
slots*.transform.SetParent (slotParent.transform);*
}
}
public void AddItem (int id)
{
CreateSlot ();
Item itemAdd = itemDatabase.GetItemByID (id);
for (int i = 0; i < slots.Count; i++)
{
if (slots*.transform.childCount <= 0)*
{
GameObject itemInstance = Instantiate(itemPrefab);
itemInstance.transform.SetParent(slots*.transform);*
itemInstance.transform.localPosition = Vector2.zero;
itemInstance.GetComponent().sprite = itemAdd.itemSprite;
Instantiate (equipButtonPrefab, slots*.transform);*
break;
}
}
}
}