- Home /
Adding 3d items on character from inventory?
Hi, Guys I am new to unity and I would like to make a simple hack and slash RPG. I found an inventory system here
http://www.theantranch.com/Unity/Entries/2010/5/17_Basic_Inventory_%26_Looting_System.html
It is a simple version of little angels inventory available on the asset store, everything worked fine but I wanted to be able to make 3d items spawn on my character so I changed the script so that it instantiates the item that is equipped at a empty gameObject named Spawn and made it delete the instance when it is taken of the character and that worked fine as well but now if I have 2 items equipped on my character e.g. the armour and the sword and I take the armour of my character it takes the 3d model sword of as well so i need to make it so that when you take of a specific item it takes of a specific 3d model but with how the code is written i don't know how.
Here is my inventory script:
// Inventory.js
// This script manages the inventory and displays the inventory items in a grid pattern
// Attach to your Inventory Manager Object
// Based on the code posted by Der Dude on the Unity3D forums: http://forum.unity3d.com/viewtopic.php?t=11865
static var statInventory : Inventory; // To set an instance of this script
enum SlotType {Head, Chest, Waist, Legs, Feet, Back, Neck, Hands, Ring, MainHand, OffHand, Bag, Empty}
// HELPER CLASSES
@System.Serializable // Our Representation of an InventoryItem
class InventoryItem {
var itemName : String; // What the item will be called in the inventory
var itemIcon : Texture; // What the item will look like in the inventory
var slotType : SlotType; // What slot the item will fit in
}
@System.Serializable // Our Representation of an Equipment Slot
class EquipmentSlot {
var slotName : String;
var slotRect : Rect;
var slotIcon : Texture2D;
var slotType : SlotType;
}
private var inventory : InventoryItem[]; // Our master inventory (private)
private var equipped : InventoryItem[]; // Our equipped inventory (private)
var equippedSlot : EquipmentSlot[]; // Our list to keep track of out Equipment Slots
private var contentArray : InventoryItem[]; // The array to contain the loot being passed to and from the LootableObject
var inventoryWidth : int; // the number of columns to display the inventory in
var inventoryLength : int; // the size of the inventory in total number of slots
var iconWidthHeight : int; // The pixel size (height and width) of an inventory slot
var spacing : int; // Space between slots (in x and y)
var offSet : Vector2; // The start position of the inventory
var emptySlot : Texture; // This will be drawn when a slot is empty
private var openInventoryWindow : boolean; // Controls OnGUI and opens/closes the inventory window
private var openLootWindow : boolean; // Controls OnGUI and opens/closes the loot window
private var openCharacterWindow : boolean; // Controls OnGUI and opens/closes the Character window
private var inventoryWindow : Rect; // The dimensions of the inventory window
private var lootWindow : Rect; // The dimensions of the loot window
private var characterWindow : Rect; // The dimensions of the Character window
private var currentLootableItem : LootableObject; // The pointer to the current lootable item being processed
private var newLootableItem : LootableObject; // The pointer to a new lootable item to be processed
function Awake () { // Setup the initial states of the variables
statInventory = this;
inventoryWindow = new Rect (270, 40, 180, 195);
lootWindow = new Rect (40, 40, 150, 230);
characterWindow = new Rect (30, 40, 180, 195);
openInventoryWindow = false;
openLootWindow = false;
openCharacterWindow = false;
// openCharacterWindow = true;
currentLootableItem = null;
inventory = new Array (inventoryLength); // Create & init the array to hold the inventory
for (var i : int = 0; i < inventory.length; i++) {
inventory[i] = null;
}
equipped = new Array (12); // Create & init the array to hold the equipped items
for (var j : int = 0; j < equipped.length; j++) {
equipped[j] = null;
}
for (var k : int = 0; k < equippedSlot.length; k++) { // Init the array for equipment slots, setting the size of the icon
equippedSlot[k].slotRect.width = iconWidthHeight;
equippedSlot[k].slotRect.height = iconWidthHeight;
}
}
function Update () { // Check for input
// This will not work on the iPhone. Quote out for touch devices.
if (Input.GetKeyUp (KeyCode.I)) { // If the "i" key is pressed...
openInventoryWindow = !openInventoryWindow; // ... toggle the inventory window.
if (!openInventoryWindow) // If the inventory window is closeing...
openLootWindow = false; // ... close the current loot window as well.
}
}
function OnGUI () {
// Loot Window
if (openLootWindow) { // If the "open loot window" toggle is true
GUI.Window (0, lootWindow, DrawLootWindow, "Loot"); // The title of this window could be passed as a String from the LootableItem
}
// Inventory Window
if (openInventoryWindow) { // If the "open inventory window" toggle is true
GUI.Window (1, inventoryWindow, DrawInventoryWindow, "Inventory"); // The title of this window could be passed as a String from the LootableItem
}
// Character Window
if (openCharacterWindow) {
GUI.Window (2, characterWindow, DrawCharacterWindow, "Character"); // Set this to player.name, not "Character"
}
}
function DrawLootWindow () { // The window function to draw the loot window
if (GUI.Button (Rect (5,5,10,10), "")) {
CloseLootWindow ();
}
var startY : int = 20;
for (var i : int = 0; i < contentArray.length; i ++) {
if (contentArray[i] != null) {
if (GUI.Button (new Rect (10, startY, 130, 30), GUIContent (" " + contentArray[i].itemName, contentArray[i].itemIcon))) {
AddItem(contentArray[i]);
contentArray[i] = null;
currentLootableItem.UpdateContentArray(contentArray);
}
startY = startY + 35;
}
}
if (GUI.Button (new Rect (25, 195, 100, 30), "Close"))
openLootWindow = false;
}
function DrawInventoryWindow () { // The window function to draw the inventory window
if (GUI.Button (Rect (5,5,10,10), "")) {
CloseInventoryWindow ();
}
var j : int;
var k : int;
var currentInventoryItem : InventoryItem; // Establish a variable to hold our data
var currentRect : Rect;
for (var i : int = 0; i < inventory.length; i ++) { // Go through each row ...
j = i / inventoryWidth; // ... divide by array by width to get rows...
k = i % inventoryWidth; // ... find the remainder by width to get columns...
currentInventoryItem = inventory[i]; // ... set this point in the matrix as our current point ...
currentRect = (new Rect (offSet.x + k * (iconWidthHeight + spacing), offSet.y + j * (iconWidthHeight + spacing), iconWidthHeight, iconWidthHeight));
if (currentInventoryItem == null) { // ... if there is no item in the j-th row and the k-th column, draw a blank texture
GUI.DrawTexture (currentRect, emptySlot);
} else {
GUI.DrawTexture (currentRect, currentInventoryItem.itemIcon);
}
// If there is an item at this location and there is a button click...
if (currentInventoryItem != null && GUI.Button (currentRect, "", GUIStyle ("label"))) {
if (Input.GetMouseButtonUp (0)) { // ... if that click is mouse button 0: equip it.
// Equip it
/////////////////////////////////////////////////////////////////////////////////////
if(currentInventoryItem.itemName == "Sword")
{
gameObject.Find("Spawn").SendMessage("ReactSword");
}
if(currentInventoryItem.itemName == "Axe")
{
gameObject.Find("Spawn").SendMessage("ReactAxe");
}
if(currentInventoryItem.itemName == "Dagger")
{
gameObject.Find("Spawn").SendMessage("ReactDagger");
}
if(currentInventoryItem.itemName == "Staff")
{
gameObject.Find("Spawn").SendMessage("ReactStaff");
}
if(currentInventoryItem.itemName == "Hammer")
{
gameObject.Find("Spawn").SendMessage("ReactHammer");
}
if(currentInventoryItem.itemName == "Battle Axe")
{
gameObject.Find("Spawn").SendMessage("ReactBattleAxe");
}
if(currentInventoryItem.itemName == "Shield")
{
gameObject.Find("Spawn").SendMessage("ReactShield");
}
if(currentInventoryItem.itemName == "Spell Book")
{
gameObject.Find("Spawn").SendMessage("ReactSpellBook");
}
if(currentInventoryItem.itemName == "Belt")
{
gameObject.Find("Spawn").SendMessage("ReactBelt");
}
if(currentInventoryItem.itemName == "Boots")
{
gameObject.Find("Spawn").SendMessage("ReactBoots");
}
if(currentInventoryItem.itemName == "Gloves")
{
gameObject.Find("Spawn").SendMessage("ReactGloves");
}
if(currentInventoryItem.itemName == "Helmet")
{
gameObject.Find("Spawn").SendMessage("ReactHelmet");
}
/////////////////////////////////////////////////////////////////////////////////////
openLootWindow = false;
openCharacterWindow = true;
var success : boolean = EquipItem (currentInventoryItem);
if (success)
inventory[i] = null;
} else if (Input.GetMouseButtonUp (1)) { // ... if that click is mouse button 1: delete it.
// Drop it
Debug.Log ("Destroying " + inventory[i].itemName);
inventory[i] = null;
}
}
}
}
function DrawCharacterWindow () {
if (GUI.Button (Rect (5,5,10,10), "")) {
CloseCharacterWindow ();
}
var currentSlot : EquipmentSlot;
var currentRect : Rect;
for (var i : int = 0; i < equipped.length; i++) {
currentSlot = equippedSlot[i];
currentRect = currentSlot.slotRect;
if (equipped[i] == null) {
GUI.DrawTexture (currentRect, currentSlot.slotIcon); // Slot Rect & Empty Slot Icon
} else {
GUI.DrawTexture (currentRect, equipped[i].itemIcon); // Slot Rect & Item Icon
if (GUI.Button (currentRect, "", GUIStyle ("label"))) {
if (Input.GetMouseButtonUp (0)) {
// ... if that click is mouse button 0: Remove/Unequip it.
var success : boolean = AddItem (equipped[i]); // Remove/Unequip it.
if (success)
{
if(contentArray.itemName == "MainHand")
Debug.Log ("trying to un-equip");
gameObject.Find("Spawn").SendMessage("RemoveMain");
}
// If it's successfully added to the inventory, remove it here
equipped[i] = null;
}
}
}
}
}
function OpenLootWindow (newLootableItem : LootableObject, newContentArray : InventoryItem[]) {
// Controls whether or not to open the loot winow and points Inventory.js to the correct currentLootableItem
if (currentLootableItem != newLootableItem) { // If the LootableItem has changed...
if (currentLootableItem != null) { // ... and if the current Lootable is not Null ...
currentLootableItem.UpdateContentArray(contentArray); // ... replace the contents with the updated contents.
currentLootableItem.LootWindowClosed ();
}
currentLootableItem = newLootableItem; // Set the new Lootable Item the Current Lootable Item
contentArray = newContentArray; // Set the Content Array to the new Content Array
openLootWindow = true; // Set the open trap on the Loot Window to "true", so if it's open is stays open, if not, it opens it
openInventoryWindow = true; // Set the open trap on the Inventory Window "true", so if it's open is stays open, if not, it opens it
openCharacterWindow = false;
} else {
openLootWindow = !openLootWindow; // This toggles the loot window
if (openLootWindow) // If the user clicks on the same Lootable Object, the window closes
openInventoryWindow = true;
openCharacterWindow = false;
}
}
function CloseLootWindow () {
openLootWindow = false;
}
function CloseCharacterWindow () {
openCharacterWindow = false;
}
function CloseInventoryWindow () {
openInventoryWindow = false;
}
function LootableObjectOutofRange () {
openLootWindow = false;
openInventoryWindow = false;
}
function AddItem (item : InventoryItem) {
for (var i : int = 0; i < inventory.length; i ++) { // Go through each row
if (inventory[i] == null) { // If the position is empty...
inventory[i] = item; // ... add the new item....
return (true); // ... and exit the function.
}
}
Debug.Log ("Inventory is full");
return (false);
}
function EquipItem (item : InventoryItem) {
for (var i : int = 0; i < equipped.length; i ++) {
if (equippedSlot[i].slotType == item.slotType) {
if (equipped[i] == null) {
equipped[i] = item;
return (true);
}
}
}
Debug.Log ("Cannot Equip this Item");
return (false);
}
function ResizeInventory (newInventoryLength) { // This code is never called at this point, but can be when you integrate it.
var oldInventory : InventoryItem[] = inventory;
inventory = new Array (newInventoryLength);
for (var i : int = 0; i < oldInventory.length; i++) {
inventory[i] = oldInventory[i];
}
for (var j : int = oldInventory.length; j < inventory.length; j++) {
inventory[i] = null;
}
}
// © 2010 Adam Buckner (aka: Little Angel) and theantranch.com (mailto: adam@theantranch.com)
// Posted on http://www.theantranch.com & http://forum.untity3d.com for use within the community
I think I need to add something in front of the line
gameObject.Find("Spawn").SendMessage("RemoveMain");
to make it work but I don't know what. Is it something like this?
if(currentSlot == "Sword")
{
gameObject.Find("Spawn").SendMessage("RemoveMain");
}
If it isn't too much to ask can someone please tell or show me what i could do to make say the sword only delete if i clicked on sword in the character window
here is a screen shot if it helps:
Your answer
![](https://koobas.hobune.stream/wayback/20220613062720im_/https://answers.unity.com/themes/thub/images/avi.jpg)