- Home /
how do i make an inventory system based on lists
So i have been trying to make an list based inventory system in Unity 2D (something like in enter the gungeon) for past 4 weeks and i have enough.
This is main thing that i atatch to player character (btw this code is from a answer by @niiicolai on another post )
The issues i have been having are as followes:
-can pick up things and put it into a list but i spawn all werpons and not only 1 that is equipped rn.
-have difficulties with finding gameobject with itemPickupScript (scripts find pickup once and then stops finding other pickups, which leads to me being unable to get other werpons than 1 thats get found first)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class InteractionMenager : MonoBehaviour
{
public KeyCode pickupKey = KeyCode.E;
public KeyCode dropKey = KeyCode.G;
public List<GameObject> weapons;
public int maxWeapons = 2;
public GameObject currentWeapon;
public Transform hand;
public Transform dropPoint;
[Header("detection parameters")]
public Transform detectionPoint;
private const float detectionRadius = 0.2f;
public LayerMask detectionlayer;
public GameObject interactionObject;
public GameObject ItemToAddPickup;
public GameObject itemToAdd;
void Update() {
if(werponng.isReloading == true){
return;
}
// SELECT WEAPONS
if (Input.GetKeyDown(KeyCode.Alpha1)) {
SelectWeapon(0);
}
if (Input.GetKeyDown(KeyCode.Alpha2)) {
SelectWeapon(1);
}
Vector2 Scalex4 = transform.localScale;
Scalex4 *= 0.25f;
ItemToAddPickup = GameObject.FindGameObjectWithTag("pickup");
itemToAdd = ItemToAddPickup.GetComponent<itemPickupScript>().ItemToAdd;
Vector2 werponposition = GameObject.Find("werponSlot").transform.position;
// PICKUP WEAPONS
if(DetectionObject()){
if (Input.GetKeyDown(pickupKey) && weapons.Count < maxWeapons) {
Debug.Log("working");
weapons.Add(itemToAdd);
GameObject ita = (GameObject)Instantiate(itemToAdd);
ita.transform.parent = hand;
ita.transform.localScale = Scalex4;
ita.transform.position = werponposition;
}
}
}
void SelectWeapon(int index) {
// Ensure we have a weapon in the wanted 'slot'
if(weapons.Count > index && weapons[index] != null) {
// Check if we already is carrying a weapon
if (currentWeapon != null) {
// hide the weapon
currentWeapon.gameObject.SetActive(false);
}
// Add our new weapon
currentWeapon = weapons[index];
// Show our new weapon
currentWeapon.SetActive(true);
}
}
bool DetectionObject(){
Collider2D obj = Physics2D.OverlapCircle(detectionPoint.position, detectionRadius, detectionlayer);
if(obj==null){
interactionObject = null;
return false;
}else{
interactionObject = obj.gameObject;
return true;
}
}
}
and i use this to get werpon from my pickup (which is another game object and i set the ItemToAdd in inspector to it)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class itemPickupScript : MonoBehaviour
{
public GameObject ItemToAdd;
}
i doubt that you will get good help on this here. You are asking a lot of work from others without providing a lot of information. Especially some info on why you cannot do it on your own would be good. Why not start at why your current approach does not work? If you just say: i've had enough of my problem, now you guys can solve it for me why would anyone be keen to do so?
Sorry it ws late at night when i was writing this and as i said i had enough of this. The issues i have been having are as followes:
can pick up things and put it into a list but i spawn all werpons and not only 1 that is equipped rn.
have difficulties with finding gameobject with itemPickupScript (scripts find pickup once and then stops finding other pickups, which leads to me being unable to get other werpons than 1 thats get found first)
And why am I actually here - beacose there is literary no usefull guide or code or paper on system like this (only 1 that i found was for unity 3d which i repurposed for unity 2d and my pickup system) and i'm also stuck on this for 4-5weeks which really frustrates me.
All of this was preety stupid of me but as i said it was really late at night when i was writing this p.s sorry for my bad english
you say in the post tho that the code is from someone else and now that is part of a tutorial.. maybe it is working well but implemented wrong, maybe you need to understand your code better or scale down your project until you get more advance.
Your answer

Follow this Question
Related Questions
How to split the objects in the inventory 0 Answers
Deleting item from inventory system... 1 Answer
Stack Problem(Please help!) 0 Answers
Inventory system help - getMouseButtomDown problem 2 Answers