Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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
1
Question by LilyTheKnight · Jul 24, 2014 at 08:22 AM · javascript

Add Component to List and then Destroy GameObject is okay?

Hello!

I wrote this code for a simple inventory, and I'm surprised not to be getting a crash or a null anywhere. In the OnTriggerEnter() function, I first add the component to my list, and then destroy the gameObject it was attached to. I read that Destroy() also destroys all the components, and since GetComponent() returns a reference to the component, it seems like when I try to access the inventoryItems later (in the Update), it should crash. But it works fine! Anyone know why?

using UnityEngine; using System.Collections; using System.Collections.Generic;

public class Inventory : MonoBehaviour { List items = new List();

 // Use this for initialization
 void Start () {
 
 }
 
 // Update is called once per frame
 void Update () {
     if (Input.GetKeyDown("space"))
     {
         Debug.Log("Inventory contents:");
         foreach (InventoryItem item in items)
         {
             Debug.Log(item.itemName);
         }
     }
 }

 void OnTriggerEnter(Collider other) {
     InventoryItem item = other.gameObject.GetComponent<InventoryItem>();
     if (item != null)
     {
         items.Add(item);
         Destroy(other.gameObject);
     }
 }

}

Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by BlackWingsCorp · Jul 24, 2014 at 09:19 AM

Yes the Destroy() command destroys the gameobject in the immediate scene and hierarchy, but not in the prefabs. So when you use getcomponent<>() you're actually getting another instance of this gameobject from the prefabs.

Take a bullets and pistol script for example: when the player shoots a bullet is called from the prefabs into the scene, then is destroyed either after some time or when it collides. When the player shoots again the instance is repeated and so on and so on... Hope this helps.

Comment
Add comment · 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 duck ♦♦ · Jul 24, 2014 at 09:39 AM 0
Share

Your first sentence is not correct, when you use GetComponent you are not getting another instance from the prefab, you're getting a reference to the specific instance that was on the object. In the case of this example, there is some weird behaviour going on due to using components in an unsupported way (i.e. trying to reference them when they are not attached to gameobjects).

avatar image
0

Answer by duck · Jul 24, 2014 at 09:45 AM

You're seeing weird behaviour because you're using Components in an unsupported way (i.e. trying to keep references to them when they're not attached to a GameObject). And while checking the itemName field seems to work fine, I'd say this seems to be more of a glitch than anything else. You'll find that if you evaluate "item == null" the result will be true, and if you try to access "item.gameObject" you'll actually get a runtime error saying the object (meaning the item itself, not the gameObject) was destroyed.

My recommendation would be: don't try and use components themselves as inventory items. You could instead use a ScriptableObject which has a reference to the item prefab. You can then create/destroy the prefab representing the item in the world whenever it's dropped or picked up, while using the reference to the scriptableobject instance as the 'true' item which can be added to inventory/chests and passed around whether it needs a real-world visual representation or not.

Comment
Add comment · 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

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.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Basic U/S question, how to do 'common' routines... 1 Answer

how to call a class on click 1 Answer

How do Ensure that a series of int's are never the same? 1 Answer

GetComponent from class 1 Answer

JS Array Object to Float 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