Hello, I have been working on this inventory system for my game and it is mostly complete apart from one problem. The code below shows the GUI and workings of the inventory. When an item in the inventory is clicked it will appear in the currently equipped box. However the item will not be removed from actual inventory (backpack if you will). Below I will give the code for the Inventory and the Item class, which is the class used to create the list of objects.
(p.s. This was converted from a javascript tutorial on making an inventory, if that has caused the problem please let me know.)
Since you may not see it after this code, I just want to say thank you for your help and have a nice day
Sorry the code is a little messy, tried to make it presentable as best i could
Inventory.cs
/* Inventory.cs coded by Philip Smyth 21/05/2013. For use in commercial products coded in C#.
* Will need to be edited to adjust with ItemClass.cs and AddItem.cs editing done.
*
*/
using UnityEngine;
using System.Collections;
using System.Collections.Generic; //To deal with lists
public class Inventory : MonoBehaviour {
public bool equipped; // Will say if there is something equipped
public ItemClass equippedItem; // The item that is equipped
public Transform playerHand; // the hand of the player, so the item can be parented to it for animations and such
public List<ItemClass> playerInventory = new List<ItemClass>(); // List of ItemClass
public Vector2 scrollView; // Will allow us to use scroll bar.
private int appear;
// Use this for initialization
void Start () {
appear = 0;
}
// Update is called once per frame
void Update ()
{
if(this.equippedItem.itemType.Equals(ItemClass.ItemType.Weapon)) //Check if there is something equipped or not.
this.equipped = true; // If true then bool equipped will be set to true
else
this.equipped = false; // If false then bool equipped will be set to false.
if(Input.GetKeyDown(KeyCode.P)) //This and if statement below used to use OnGui only when pause menu is
{appear = appear + 1;} //active on another script
if(appear == 2)
{appear = 0;}
}
void OnGUI()
{
//Inventory Display Management.
if(appear == 1){
GUILayout.BeginArea(new Rect( // Will begin the GUI Area. In the top left corner
(float) (Screen.width-500), // X
(float)0, // Y
(float)300, // Width
(float)300)); // Height
GUILayoutOption[]optionArray1 = new GUILayoutOption[] {GUILayout.Width((float)500), GUILayout.Height((float)500)};
this.scrollView = GUILayout.BeginScrollView(this.scrollView, optionArray1); // Start scrol view and assign it to the scrollView variable
for(int i = 0; i < this.playerInventory.Count; i++) // ran for every item in the inventory
{
GUILayout.BeginHorizontal(new GUILayoutOption[0]); // begin the horizaontal area
if(GUILayout.Button(this.playerInventory*.icon, new GUILayoutOption[0]) // If the button is clicked...*
_ &&(this.playerInventory*.itemType.Equals(ItemClass.ItemType.Weapon)) // … and the item is represented is a weapon…_
_ && !this.equipped) // … and there is nothing currently equipped._
_ {_
_ this.equippedItem = this.playerInventory; // assign the inventory item to equippedItem*_
* Transform thing; //used for the instantiation*
_ thing = (Transform)Object.Instantiate(this.playerInventory*.itemPrefab, // Instantiate the prefab of the weapon*
* this.playerHand.position, // and maintain the prefabs rotation*
* Quaternion.identity);
thing.parent = this.playerHand; // set the parent to the player hand(for animations etc.)*_
_ this.playerInventory.RemoveAt(i); // Remove the item from the inventory THIS IS WHERE I THINK ITS WRONG*_
* // dont need to continue processing item, so jump back to beginning of function.*
* return; *
* }*
_ GUILayout.Box(this.playerInventory*.name, new GUILayoutOption[0]); //If not clicked yet, display name*
* GUILayout.EndHorizontal(); // End the horizontal area*
GUILayout.Box(this.playerInventory*.description, new GUILayoutOption[0]); // Display the description.*_
* }*
* GUILayout.EndScrollView(); // End the scroll view*
* GUILayout.EndArea(); // End the area.*
* // Weapon Slot Display*
* if(GUI.Button(new Rect( *
* (float)((30)), // X *
* (float)(Screen.height - 70), // Y*
* (float) 50, // Width *
* (float) 50), // Height*
* this.equippedItem.icon) // Item clicked an icon*
* && this.equippedItem.itemType == ItemClass.ItemType.Weapon){ // If clicked and a weapon*
* for(int x = 0;x < 1; x++ ){*
* this.playerInventory.Add(this.equippedItem); // Add item to inventory*
* }*
* //Object.Destroy(GameObject.FindGameObjectWithTag(“Weapon”)); // Destroy instantiated prefab WASNT NEEDED*
* this.equippedItem = new ItemClass(); // replace with blank item*
* } *
* }*
* }*
}
//End
Then the ItemClass Scrips
ItemClass.cs
/* ItemClass.cs coded by Philip Smyth 21/05/2013. For use in Commercial products coded in C#.
* This is not applied to a game object however it is called by both Inventory.cs and AddItem.cs
* Little editing to be required for this script. However further infor can be included.
*/
using UnityEngine;
using System.Collections;
[System.Serializable]
public class ItemClass{ // make public to be accessed by other scripts
* //Feilds*
* public string name; // The name of the item*
* public string description; // Describe the item*
* public Texture2D icon; // The 2D icon that will be used in the inventory and hotbar.*
* public int id; // ID of the item for future editing*
* public Transform itemPrefab; // The 3D model of the item.*
* public ItemType itemType; // The type of the item (weapon, armour, other etc.)*
* public int dmg;*
* //Nested Types*
* public enum ItemType // The available item types to be chosen.*
* {*
* None,*
* Weapon,*
* Other,*
* Armour*
* }*
}
//End