• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
Question by zmar0519 · Apr 18, 2011 at 05:06 PM · destroygetcomponentcomponentinventory

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 c$$anonymous$$ld, 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. T$$anonymous$$s 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 = t$$anonymous$$s.gameObject.transform.position; objectsInBag[j].transform.rotation = t$$anonymous$$s.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 &lt; 10; i++) 
     {
         for(j = 0; j &lt; 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; t$$anonymous$$s.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(t$$anonymous$$s.gameObject);

}

Comment

People who like this

0 Show 0
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
Best Answer

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));
}
Comment
GesterX

People who like this

1 Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image zmar0519 · Apr 18, 2011 at 06:08 PM 0
Share

Thank you so much.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

Simple question about OnMouseButtonAsUp 1 Answer

'thrust' is not a member of UnityEngine.Component 1 Answer

Multi similar script Components same game object 1 Answer

Reference Not Set toInstance of Object 1 Answer

Inventory problems (C#) [edited to be more clear] 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges