Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

RPG Inventory Source

Discussion in 'Made With Unity' started by unitrix, May 6, 2010.

  1. Zu01

    Zu01

    Joined:
    Aug 17, 2010
    Posts:
    95
    Anyone?
     
  2. unitrix

    unitrix

    Joined:
    Mar 19, 2010
    Posts:
    279
    Do a for loop that goes through the inventory arrays and then have it check if the item you are looking for matches the itemname in the array slot.
     
  3. Butterzroxx

    Butterzroxx

    Joined:
    Aug 30, 2011
    Posts:
    9
    Quick question, when I use the FPSWalkerEnhanced, it works perfectly except when I hold down the right mouse button to look around, it only moves on the X-axis, Not the Y, so I can't look up or down. I've tried everything. Not sure what I'm doing wrong.
     
  4. Zu01

    Zu01

    Joined:
    Aug 17, 2010
    Posts:
    95
    weird, but thats what you get with all this new unity crap, i miss the old controllers too.
     
  5. patrick_vargas

    patrick_vargas

    Joined:
    Oct 13, 2011
    Posts:
    11
    zu, you...your script is cool...but it's no flexible...think when you want sell 100 items...you'll need do that code 100 times...
    when you need to do some routines like that, use "for" statements....
    think about it....look this code...once you increase the number of items and the array(both should be the same size), you don't need do nothint...just add the item in the inspector..
    if you want use the code...just clean some parameters...like modalwindow, customs GUISkin and sounds.
    btw...this is only a tip....like i said...you code is cool.
    tnks

    var _Shopping = false;

    var _NumerOfItems : int;

    var _Item : Transform[];

    public var _NewItem : Array;

    var _ItemArray : Array;



    var inv: Inventory;

    var _ShoppingItems : Array;

    var Money : int = 1050;



    public var _ClickSoundSelect : AudioSource;



    public var _Slot : boolean[];



    public var _GuiGame : GUISkin;

    //private var _WindowShopping = Rect((Screen.width/4.5), (Screen.height/6),750,450);

    private var _WindowShopping = Rect(0, 0,Screen.width,Screen.height);



    var ypos : float = 10;

    var xpos : float = 10;



    function OnMouseDown()

    {

    _Shopping = true;

    _ClickSoundSelect.Play();

    }



    function Awake()

    {



    _ShoppingItems = new Array(_NumerOfItems);

    _NewItem = new Array(_NumerOfItems);

    _ItemArray = new Array(_NumerOfItems);



    for(var i = 0; i < _ShoppingItems.length; i++)

    {

    Scripts = _Item.GetComponent("ItemScript");

    _NewItem = new InventoryItem();

    _NewItem.itemname = Scripts.itemname;

    _NewItem.itemDescription = Scripts.itemDescription;

    _NewItem.itemtex = Scripts.itemtex;

    _NewItem.itemtype = Scripts.itemtype;

    _NewItem.equipmenttype = Scripts.equipmenttype;

    _NewItem.usable = Scripts.usable;

    _NewItem.itemweight = Scripts.itemweight;

    _NewItem.droppable = Scripts.droppable;

    _NewItem.itemmodel = Scripts.itemmodel;

    _NewItem.itemValue = Scripts.itemValue;



    _NewItem.itemstacksize = Scripts.itemstacksize;

    _NewItem.itemstacklimit = Scripts.itemstacklimit;

    _NewItem.showStack = Scripts.showStack;



    _NewItem.bagsize = Scripts.bagsize;

    _NewItem.showBag = Scripts.showBag;

    _NewItem.BagItem = Scripts.BagItem;

    _ShoppingItems = _NewItem;

    }

    }



    function OnGUI()

    {

    if(_Shopping)

    {

    _WindowShopping = GUI.Window(GetInstanceID(), _WindowShopping, WindowShopping, "Shopping", _GuiGame.customStyles[0]);



    }

    }





    function CallItems(_ItemArray, xpos, ypos)

    {

    var modalwindow : YesNo = GameObject.Find("Shopping").GetComponent("YesNo");

    Scripts = _Item[_ItemArray].GetComponent("ItemScript");



    GUI.Box(Rect(xpos, ypos, 250, 55),"",_GuiGame.customStyles[0]);

    if (GUI.Button(Rect (xpos + 200, ypos + 1, 50, 15),"Buy", _GuiGame.customStyles[9]))

    {

    _Slot[_ItemArray] = true;

    modalwindow.ArrayNumber = _ItemArray;

    modalwindow._YesNo = true;

    modalwindow._Title = "Buying";



    //Sound.

    _ClickSoundSelect.Play();



    //Show the Attributes in the Modal Windows Confirmation.

    modalwindow._ItemName = _ShoppingItems[_ItemArray].itemname;

    modalwindow._EquipmentLV = _ShoppingItems[_ItemArray].equipmentLV;

    modalwindow._EquipmentDescription = _ShoppingItems[_ItemArray].itemDescription;

    }



    if(_Slot[_ItemArray] modalwindow._Yes _ShoppingItems[_ItemArray].itemValue < Money _ShoppingItems[_ItemArray] != null)

    {

    Inventory.statInventory.AddItem(_ShoppingItems[_ItemArray]);

    Money -= _ShoppingItems[_ItemArray].itemValue;

    modalwindow._Yes = false;

    modalwindow._YesNo = false;

    _Slot[_ItemArray] = false;



    Debug.Log(_ItemArray);

    }

    if (_ShoppingItems[_ItemArray]!= null)

    {

    GUI.Button(Rect (xpos, ypos + 9, 52, 52),"", _GuiGame.customStyles[23]);

    GUI.DrawTexture (Rect(xpos,ypos + 9,48,48),_ShoppingItems[_ItemArray].itemtex);

    GUI.Label(Rect(xpos, ypos - 3, 200, 25),_ShoppingItems[_ItemArray].itemname, _GuiGame.customStyles[35]);

    GUI.Label(Rect(xpos + 180, ypos + 36, 100, 25),_ShoppingItems[_ItemArray].itemValue.ToString() + " M", _GuiGame.customStyles[35]);

    GUI.Label(Rect(xpos + 50, ypos + 5, 170, 50),_ShoppingItems[_ItemArray].itemDescription, _GuiGame.customStyles[35]);

    }



    }

    var scrollPosition : Vector2 = Vector2.zero;

    function WindowShopping(windowID : int)

    {

    GUI.Label(Rect(10, 250 , 150, 20)," Money " + Money);

    if (GUI.Button(Rect (Screen.width - 15,Screen.height - Screen.height + 2,12,12),"", _GuiGame.customStyles[2]))

    {

    _Shopping = false;

    _ClickSoundSelect.Play();

    }



    GUI.BeginGroup(Rect(20,50,285,450), "Items", _GuiGame.customStyles[0]);

    scrollPosition = GUI.BeginScrollView (Rect (0,20,285,400),//Scrollview

    scrollPosition, Rect (0, 0, 220, (_ShoppingItems.length * 65.04)));//Conteudo



    var ini : float = 30;

    var fim : float = 65;

    var total : float;

    [COLOR="red"]for(var i = 0; i < _ShoppingItems.length; i++)[/COLOR]

    {

    CallItems(i,15, ini);

    ini = ini += fim;

    }

    GUI.EndScrollView ();

    GUI.EndGroup();



    /*GUI.BeginGroup(Rect(500,50,285,450), "Inventory", _GuiGame.customStyles[0]);

    var inv : Inventory = GameObject.Find("Inventory").GetComponent("Inventory");

    inv.inventorystate = true;

    GUI.EndGroup();*/

    }
     
    Last edited: Nov 4, 2011
  6. patrick_vargas

    patrick_vargas

    Joined:
    Oct 13, 2011
    Posts:
    11
    btw....you can do the same thing with looting...
     
  7. patrick_vargas

    patrick_vargas

    Joined:
    Oct 13, 2011
    Posts:
    11
    here's my inventory.
     

    Attached Files:

    Last edited: Nov 4, 2011
  8. patrick_vargas

    patrick_vargas

    Joined:
    Oct 13, 2011
    Posts:
    11
    Last edited: Nov 4, 2011
  9. 3CardMonte

    3CardMonte

    Joined:
    Apr 3, 2012
    Posts:
    1
    Im trying to add this kind of inventory into my FPSRPG but I have the screen locked on the cursor so clicking the inventory wont work. I need to know how to code the inventory so that I can open it by pressing a key. Can someone help? Im new to unity and dont really know javascript.
     
  10. BrandonErvin

    BrandonErvin

    Joined:
    Feb 19, 2013
    Posts:
    2
    Can anyone help with an issue with not being able to drop inventory items in game after replacing the item prefab? The project works fine when I first run it but after replacing the object prefab I get this error:

    MissingReferenceException: The object of type 'Transform' has been destroyed but you are still trying to access it.
    Your script should either check if it is null or you should not destroy the object.
    UnityEngine.Object.Internal_InstantiateSingle (UnityEngine.Object data, Vector3 pos, Quaternion rot) (at C:/BuildAgent/work/842f9557127e852/Runtime/ExportGenerated/Editor/UnityEngineObject.cs:41)
    UnityEngine.Object.Instantiate (UnityEngine.Object original, Vector3 position, Quaternion rotation) (at C:/BuildAgent/work/842f9557127e852/Runtime/ExportGenerated/Editor/UnityEngineObject.cs:50)
    Inventory.addnewitem () (at Assets/Scripts/Inventory.js:539)
    Inventory.OnGUI () (at Assets/Scripts/Inventory.js:311)

    I also wanted to note for the people having issues saving the inventory items ect there's an awesome product out there called Unity Serializer that lets you save pretty much everything on a game object at once without having to code it. http://whydoidoit.com/unityserializer/
     
    Last edited: Feb 19, 2013
  11. pvargas

    pvargas

    Joined:
    Nov 26, 2012
    Posts:
    5
    if (Input.GetKeyDown(KeyCode.I))
    {
    _InventoryState = !_InventoryState;
    }
     
  12. SpaceFiveFive

    SpaceFiveFive

    Joined:
    Nov 6, 2013
    Posts:
    27
    How would I add this to an already existing game?
     
  13. Ngoadam123

    Ngoadam123

    Joined:
    Aug 24, 2013
    Posts:
    3
    How can you make it so when you click on an object it will give you a item?
     
  14. Ngoadam123

    Ngoadam123

    Joined:
    Aug 24, 2013
    Posts:
    3
    Do you put it in your inventory script or your players script?
     
  15. BANHAMMERxXx

    BANHAMMERxXx

    Joined:
    Dec 12, 2014
    Posts:
    3
    I have found many bugs i need help to fix
     
  16. BANHAMMERxXx

    BANHAMMERxXx

    Joined:
    Dec 12, 2014
    Posts:
    3
    I need help setting it up im having soooo many errors