- Home /
Destroy(GetComponent(...)) not working
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[i]; texturesForBag = new Texture2D[i]; InvWindowRect = Rect(10,10,(slotSize+slotSpacing)*4+50, 400); } 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);
scrollPosition = GUI.BeginScrollView (Rect (15, 90,(slotSize+slotSpacing)*4+17, 200), scrollPosition, Rect (10, 10, (slotSize+slotSpacing)*4, (slotSize+slotSpacing)*10));
for(i = 0; i < 10; i++)
{
for(j = 0; j < 4; j++)
{
if(GUI.Button(Rect(10+(j*(slotSize+slotSpacing)), 10+(i*(slotSize+slotSpacing)), slotSize, slotSize), texturesForBag[(j*10)+i]))
{
EquipItem(objectsInBag[(j*10)+i]);
}
}
}
GUI.EndScrollView ();
} function Update() { if(currentItem.GetComponent(Item)) currentItem.Destroy(GetComponent(Item)); }
Here is my Item Code:
var icon : Texture2D;
var Name : String;
And Here is my equippable code:
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);
}
Answer by Wolfram · Apr 18, 2011 at 05:13 PM
It seems to me you are trying to remove the component from the current GO, not the item you want. So change it to:
function Update()
{
if(currentItem.GetComponent(Item))
Destroy(currentItem.GetComponent(Item));
}
Your answer
Follow this Question
Related Questions
Reference Not Set toInstance of Object 1 Answer
Inventory problems (C#) [edited to be more clear] 1 Answer
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Using GetComponent in multiple scripts for same component? C# 2 Answers
Change the Halo Component (not the default) in a light in C#??? 0 Answers