Search Unity

scripting a inventory in c#

Discussion in 'Scripting' started by antoniog9430, Sep 3, 2015.

  1. antoniog9430

    antoniog9430

    Joined:
    Mar 15, 2014
    Posts:
    13
    I have been working on trying to create a inventory system and crafting system for my player , i downloaded the invenrory master asset from the store but i dont like it at all because when a item is added to the inventory slot its a item prefab which the modular building system for somereason doesnt work with that .

    I was wondering what steps do i need to take in order to create or script a inventory for my player i have the items script which has
    The item icon
    The items name
    the items gameobject
    The ability of the item
    The type of item
    And the rarity of the item

    All of those have a script
    My item type script item ability item rarity and so on
    I also have a script that defines the id number for the items as well as defines the item id for the items that get picked up

    I already have the inventory screen set up with the slots that are numbered 1-28 and is working now i just need to beable to pick the items up and add them to my inventory but when they get added i want them all to have their own names so if i pick up a apple it will be the game objects prefab name which is appleprefab

    Any and all help would be awesome
     
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    have you tried working before with classes that don't inherit from mono behaviour?

    i.e. an item/apple class which holds the pertinent information about "what this item/apple is", rather than an instantiated gameobject?
     
  3. zotey

    zotey

    Joined:
    Nov 14, 2012
    Posts:
    26
    what lefty righty said is correct but doesn't answer the question in total.
    You should make a list or dictionary to where you can add all items to so you get a list of your items, after that is done you can make another list for your inventory and grab the items out of the item list.
     
  4. antoniog9430

    antoniog9430

    Joined:
    Mar 15, 2014
    Posts:
    13
    These are my scripts item database has all my items in it that will be available in game and item script is the one that goes on the item prefab
     

    Attached Files:

  5. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    well no, it's asking for more information :p

    and from those scripts the answer appears to be "no" but you're heading in the right direction from the looks of the item script.


    in unity you tend to start with "everything is a gameobject" and "all scripts are components inheriting from mono behaviour" which is fine up to a point. Then you run into "but I don't want it to exist in the scene, but I want to know about it" and you have to split things up.

    Code (csharp):
    1.  
    2. using UnityEngine;
    3.  
    4. public class Item
    5. {
    6.     public string name;
    7.     public int id;
    8.     public GameObject go;
    9.  
    10.     public Item()
    11.     {
    12.         // handle constructor stuff here    
    13.     }
    14. }
    15.  
    note the complete lack of inheritance. This is just a "normal" c# object, you can store these in your inventory lists/arrays and hold the information about what is in the container. The "go" is used when you want it to exist in the world, you can instantiate the gameobject, i.e. take something out of the container and put it into the characters hand / drop it into the scene etc.

    The next question is, "ok, we have these class/objects to hold the item information but where do we store them?!"... and that is when we introduce ScriptableObjects.

    https://unity3d.com/learn/tutorials/modules/beginner/live-training-archive/scriptable-objects
    http://docs.unity3d.com/ScriptReference/ScriptableObject.html
    http://burgzergarcade.net/scriptableobjects-as-databases-in-unity/

    The bergzerg arcade is a good example, but the html code on the webpage is borked :( They have done a much longer (more complicated and in depth) tutorial on items systems on their youtube channel.