#Inventory System

1 messages · Page 1 of 1 (latest)

lament dagger
#

Oh Alr

#

U made one

#

Alr

#

I’ll be on my pc in a bit

#

Maybe 8 pm EST

lament dagger
#

Alright

lament dagger
#

im on my pc

#

script is coming right up

#
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;

public class CrateManager : MonoBehaviour
{

    public CrateMaterial[] secondaryCrateMaterial;
    public CrateMaterial[] primaryCrateMaterial;
    public WeaponType[] weaponTypes;
    public TMP_Text weaponNameObject;
    public Transform crateObjectSpawn;
    public KeyCode openCrateKey;
    private GameObject previousCrateWeapon; // Store reference to the previously instantiated weapon


    private void Update()
    {
        if (Input.GetKeyDown(openCrateKey))
        {
            OpenCrate();
        }
    }


    public void OpenCrate()
    {
        if (previousCrateWeapon != null)
        {
            Destroy(previousCrateWeapon);
        }

        int secondaryMaterial = Random.Range(0, 5);
        int primaryMaterial = Random.Range(0, 5);
        
        int weapon = 0;

        string weaponName = $"{secondaryCrateMaterial[secondaryMaterial].materialPrefix}" + " " + $"{primaryCrateMaterial[primaryMaterial].materialName}" + " " + $"{weaponTypes[weapon].weaponName}";

        weaponNameObject.text = weaponName;

        GameObject crateWeapon = Instantiate(weaponTypes[weapon].weaponObject, crateObjectSpawn);

        Debug.Log("Crate weapon instantiated"); // Debug log
        crateWeapon.transform.localPosition = Vector3.zero;

        Renderer[] renderers = crateWeapon.GetComponentsInChildren<Renderer>();

        foreach (Renderer renderer in renderers)
        {
            if (renderer.gameObject.CompareTag("SecondaryPart"))
            {
                renderer.material = secondaryCrateMaterial[secondaryMaterial].material;
            }
            else if (renderer.gameObject.CompareTag("PrimaryPart"))
            {
                renderer.material = primaryCrateMaterial[primaryMaterial].material;
            }
        }

        previousCrateWeapon = crateWeapon;
    }
}
#

this is the crate manager script

#

that spawns the weapons

#

this is the weapontype monobehaviour that is put on every weapon type

using UnityEngine;

public class WeaponType : MonoBehaviour
{
    public string weaponName;
    public GameObject weaponObject;
    public GameObject secondaryWeaponPart;
    public GameObject primaryWeaponPart;
}
#

yeah

#

but how would I make the list

#

and i want the inventory to have weapons, materials, and other things

#

no like iron and stuff

#

those type of materials

#

resources

#

i dont know how to make one tho

#

can u help me with that

#

please

#

no

#

i mean the list

#

i just need help making a simple list

#

that stores resources, weapons, and consumables

#

like more elaborate tho

#

cuz i have no idea

#

what that means

#

and where i put it

#

okay

#

ill try

#

im not good with inventory tutorials

#

cuz none of them are even similar to what i am trying to do

#

the UI just confuses me

#

because most of them start with finding an empty slot

#

okay

#

still am confused even when I watch the videos

#

thats why I asked you

#

i thought you would walk me through it

#

i know you dont have a lot of time

#

but i was just hoping

#

im already confused

#

yeah but what about the prefab

#

and what about storing the secondary and primary materials on the object

#

see like this is the part where I wanna unalive myself cuz its so confusing

#

yeah but what about the material part

#

how is it gonna store it

#

yeah but how is it gonna add the object

#

can u walk me through it?

#

lemme show u what i have done

#

wait

#

can i do something like this

#

yes but how

#

can u write me a sample script of what that might look like

#

would this work?

#
using UnityEngine;

[CreateAssetMenu(fileName = "New Item", menuName = "Inventory/Item")]
public class Item : ScriptableObject
{
    public new string name = "New Item";
    public Sprite icon = null;
    public GameObject prefab = null;
    public bool isStackable = false;
    public bool isWeapon = false;

    [Header("Weapon Properties")]
    public float damage = 0f;
    public float staminaUsage = 0f;
    public float durability = 0f;

    [Header("Item Properties")]
    public float weight = 0f; // New field for item weight
}

#

or does it need to be just Item

#

if i was to create that class

#

like remove scriptable object

#

im now confused

#

lets stay on the same path of class

#

how would I do that

#

no scriptable objects rn

#
using UnityEngine;

[CreateAssetMenu(fileName = "New Item", menuName = "Inventory/Item")]
public class Item
{
    public new string name = "New Item";
    public Sprite icon = null;
    public GameObject prefab = null;
    public bool isStackable = false;
    public bool isWeapon = false;

    [Header("Weapon Properties")]
    public float damage = 0f;
    public float staminaUsage = 0f;
    public float durability = 0f;

    [Header("Item Properties")]
    public float weight = 0f; // New field for item weight
}

#

?

#

yes

#

ill get rid of that

#

yeah

#

but now what

#

confusion

#

👍

#

yeah but is that literally it?

#

i feel like theres more to it

#
using Unity.VisualScripting;
using UnityEngine;

[CreateAssetMenu(fileName = "New Item", menuName = "Inventory/Item")]
public class Item
{
    public string itemName = "New Item";
    public Sprite icon = null;
    public GameObject prefab = null;
    public bool isStackable = false;
    public bool isWeapon = false;




    [Header("Item Properties")]
    public float weight = 0f; // New field for item weight
}
#

so this is fine?

#

really?

#

and where can I see this

#

do i have to assign this to a prefab or object

#

ugh

#

like what do i do with the Class

#

@wide pelican

#

WAIt

#

do I have the child class on the same script?

fading rain
lament dagger
#

wut

fading rain
#

You could cause dangling reference pointer if you accidentally delete your scriptable object's gameobject.

lament dagger
#

holy moly

#

okay

#

but what about the classes

#

do i keep a child class inside the class script?

fading rain
#

Item could be a interface.

#

or a struct hmm

lament dagger
#

idk what interface is

fading rain
#

don't worry about it too much, it's basically a contract declaration. You create a template that says I promise you these method exist. Then your class that inherits the interface must implement the provided method.

lament dagger
#

can u explain what u did

#

poco

#

wtf is poco

fading rain
#

The method implementation under OpenCrate() could be optimize a bit.

lament dagger
#

lol

#

what is item

#

wait no ItemType

#

im confused

#

woohoo

fading rain
#

Oof... Guess we'll have to wait until you're unmuted I'm afraid.