Hi, everybody. I have an inventory script that I wrote, but it has a problem. The logic I have is you click on it, and it then becomes a child, and is disabled. Then, in the window, when the user clickes on the icon, it goes to a specified position. However, I want to destroy the item component so that it does not get added to the inventory again if the user clicks on it. This is where the `Destroy(GetComponent(Item))` does not get called, so it can be added to the inventory again. Here is my Inventory code:
var slotSize : float;
var slotSpacing : float;
var equipItemPos : Transform;
var Skin : GUISkin;
private var scrollPosition : Vector2 = Vector2.zero;
private var objectsInBag : GameObject[];
private var texturesForBag : Texture2D[];
private var nextObjectInt : int = 0;
private var currentItem : GameObject;
private var InvWindowRect : Rect;
function Awake()
{
var i : int;
i = 10*4;
objectsInBag = new GameObject*;*
_texturesForBag = new Texture2D*;*_
<em>_InvWindowRect = Rect(10,10,(slotSize+slotSpacing)*4+50, 400);_</em>
_*}*_
_*function AddObject(j : GameObject)*_
_*{*_
_*objectsInBag[nextObjectInt] = j;*_
_*texturesForBag[nextObjectInt] = j.GetComponent(Item).icon;*_
_*nextObjectInt++;*_
_*}*_
_*function EquipItem(i : GameObject)*_
_*{*_
_*currentItem = i;*_
_*for(var j = 0; j < objectsInBag.length; j++)*_
_*{*_
_*if(objectsInBag[j] != null)*_
_*{*_
_*objectsInBag[j].transform.position = this.gameObject.transform.position;*_
_*objectsInBag[j].transform.rotation = this.gameObject.transform.rotation;*_
_*objectsInBag[j].active = false;*_
_*}*_
_*}*_
_*i.transform.position = equipItemPos.position;*_
_*i.transform.rotation = equipItemPos.rotation;*_
_*i.active = true;*_
_*}*_
_*function OnGUI()*_
_*{*_
_*GUI.skin = Skin;*_
_*InvWindowRect = GUI.Window (0, InvWindowRect, InventoryWindow, "Inventory");*_
_*}*_
_*function InventoryWindow(windowID : int)*_
_*{*_
_*if(currentItem != null)*_
_*GUI.Label(Rect(41, 292, 250, 30), "Current Item: "+currentItem.GetComponent(Item).Name);*_
<em>_scrollPosition = GUI.BeginScrollView (Rect (15, 90,(slotSize+slotSpacing)*4+17, 200), scrollPosition, Rect (10, 10, (slotSize+slotSpacing)*4, (slotSize+slotSpacing)*10));_</em>
_*for(i = 0; i < 10; i++)*_
_*{*_
_*for(j = 0; j < 4; j++)*_
_*{*_
<em>_if(GUI.Button(Rect(10+(j*(slotSize+slotSpacing)), 10+(i*(slotSize+slotSpacing)), slotSize, slotSize), texturesForBag[(j*10)+i]))_</em>
_*{*_
<em>_EquipItem(objectsInBag[(j*10)+i]);_</em>
_*}*_
_*}*_
_*}*_
_*GUI.EndScrollView ();*_
_*}*_
_*function Update()*_
_*{*_
_*if(currentItem.GetComponent(Item))*_
_*currentItem.Destroy(GetComponent(Item));*_
_*}*_
_*```*_
_*<p>Here is my Item Code: </p>*_
_*```*_
_*var icon : Texture2D;*_
_*var Name : String;*_
_*```*_
_*<p>And Here is my equippable code:</p>*_
_*```*_
_*var pickUpKey : KeyCode;*_
_*var GText : GUIText;*_
_*function OnTriggerStay(col : Collider)*_
_*{*_
_*if(col.gameObject.tag == "Player")*_
_*{*_
_*GText.enabled = true;*_
_*if(Input.GetKeyUp(pickUpKey))*_
_*EquipItem();*_
_*}*_
_*}*_
_*function OnTriggerExit()*_
_*{*_
_*GText.enabled = false;*_
_*}*_
_*function EquipItem()*_
_*{*_
_*transform.parent = FindObjectOfType(InventoryGUI).gameObject.transform;*_
_*this.gameObject.SetActiveRecursively(false);*_
_*transform.position = Vector3(transform.parent.GetComponent(InventoryGUI).equipItemPos.position.x, transform.parent.GetComponent(InventoryGUI).equipItemPos.position.y-10, transform.parent.GetComponent(InventoryGUI).equipItemPos.position.z);*_
_*FindObjectOfType(InventoryGUI).AddObject(this.gameObject);*_
_*```*_
_*<p>}</p>*_