Help with an inventory system

So as said in the title I need help with making an inventory system using unityscript. I have tried searching the forum and Google since Wednesday but I didn’t find any good tutorials etc. to suit my needs. Inventory system is going to be used in first person adventure game, but I guess it will be easy to use in other type of games too.

How I thought the system would work: Four scripts: inventory, pick up, equip item and item script (which is attached to the gameobject). So far I have started the pick up script. Script is attached to the main camera, when you look at gameobjective which has tag “item” you will see message “press E to pick up” (Later on adding something like item name and stats would be awesome). When you press E, the gameobject will be destroyed. Gameobjects that you can pick up have item script which currently only has two variables: name and id.

Pick up script: after the item gets destroyed (deleted from scene) would it be possible to check the id (or name of the prefab etc.) and add it to inventory. I had the destroy part but I decided to remove it because I messed it up somehow and I couldn’t get it working.

var rayLenght : int = 10;
var ShowText : boolean = false;


function Update ()
{   
    var hit: RaycastHit;
    var fwd = transform.TransformDirection(Vector3.forward);
   
    if (Physics.Raycast(transform.position, fwd, hit, rayLenght))
    {
        if(hit.collider.gameObject.tag == "Item")           
            ShowText = true;
    }
        else {
            ShowText = false;
    }   

       
    }               


function OnGUI() {
    if (ShowText) {
        GUILayout.Label("Press E to pick up");                           
    }
}

Inventory: I’m not sure how arrays/arraylists work but how I thought it would go: there is some place (array?) which consists of item id’s (that are in the item script, on item prefabs). When you press lets say tab, it opens ui (haven’t thought of that because of the new ui system I have no knowledge about) it shows the items you have (script checks the id and displays right icon) and you could equip for example a sword which would show in your hand (Not necessary atm, I would like to have other things working first). Also you should be able to remove item from inventory, check item id, spawn it in front of player.

So if anyone could guide me what should I do and how, it would be more than appreciated! Any tutorials, videos and example scripts would be awesome.

Obviously I am a beginner and some people may find these threads annoying, but I’m trying to learn scripting. Also I apologize for any grammatical mistakes and typos.

OK, I’m going to stop you right here.

You need to pick up some of the fundamentals of programming, of which an array is a very fundamental part of it.

An array is one of those fundamental things.

An array is, as the word implies, an ordered arrangement of objects. The type of the array is what the type of the objects being ordered are, and the order is… in an indexed order from 0 to Length - 1. In memory they’re ordered in the same way as well.

Things like Lists and Dictionaries will wrap around an array and relate them in more robust manners (Lists allow for easy resizing, Dictionaries allowing accessing a specific index by a hash instead of by index).

If you want my suggestion. Scale back a little bit, and learn the fundamentals.

1 Like

I use empty game objects for each inventory item and attach icons, etc, to them in a script. It wouldn’t be necessary to use a game object, I just did so I could make changes in the inspector. I parent them to an inventory object and make them Don’tDestroyOnLoad in an init scene. I put them in a generic list in my inventory manager script. I can’t really go into detail, but it would be a good idea to study generic lists through a search. They work the best for me because I don’t have to type cast when I remove them.

1 Like