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
0
Question by cow_co · Aug 05, 2014 at 09:37 AM · errorgameobjectinstantiateprefabdestroy

Instantiating prefabs: "The object of type GameObject has been destroyed".

I am using this code to equip items on the player:

     //Equipping items on the character's model
     public void EquipItems()
       {
         //runs through each location where the items will be equipped on the player character model
         for (int i = 0; i < 5; i++)
         {
             //if the equip location exists
             if (itemLocation [i] != null)
             {
                 //destroys any existing equipped items if they don't match the newly-equipped items
                 foreach (Transform child in itemLocation[i].transform)
                 {
                     if (child != null)
                     {
                         if (child.gameObject.name != equippedItems[i].itemPrefab.name && child.gameObject.tag == "Item")
                         {
                             GameObject.Destroy(child.gameObject);
                         }
                     }
                 }
                 //places the new items in their locations
                 equippedItemsClones[i] = Instantiate (equippedItems [i].itemPrefab, itemLocation[i].transform.position, itemLocation[i].transform.rotation) as GameObject;
                 equippedItemsClones[i].transform.parent = itemLocation[i].transform;
                 Debug.Log("instantiating " + equippedItems[i].itemName);
             }
         }
   }

But, when I run it, I pick up an item from the ground, try to equip it, and get shown this error message:

MissingReferenceException: The object of type 'GameObject' has been destroyed but you are still trying to access it. Your script should either check if it is null or you should not destroy the object. UnityEngine.Object.Internal_InstantiateSingle (UnityEngine.Object data, Vector3 pos, Quaternion rot) (at C:/BuildAgent/work/d63dfc6385190b60/artifacts/EditorGenerated/UnityEngineObject.cs:74) UnityEngine.Object.Instantiate (UnityEngine.Object original, Vector3 position, Quaternion rotation) (at C:/BuildAgent/work/d63dfc6385190b60/artifacts/EditorGenerated/UnityEngineObject.cs:84) CharacterClass.EquipItems () (at Assets/scripts/Classes/CharacterClass.cs:94) InventoryScript.Update () (at Assets/scripts/GUIScripts/InventoryScripts/InventoryScript.cs:63)

I don't know what's gone wrong.

Comment
Add comment · Show 1
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 KiraSensei · Aug 05, 2014 at 09:38 AM 0
Share

Please tell us what is line 63 of the script InventoryScript.cs because we don't know where it is if you post only a fraction of the code.

But since you check intemLocation[i], I assume that it is equippedItems[i] that has a problem...

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Tehnique · Aug 05, 2014 at 09:50 AM

Most probably, your list of equipedItems is populated based on references to the transform children that are tagged as items. You then go and destroy some of those children, and then try to access them again from equipedItems, but since equipedItems are references, and the referenced object was destroyed, you get that error.

Either instantiate your clones, and then call Destroy, or check that equipedItems[i] is != null before instantiating.

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

Accessing children of an instance doesn't work every time 1 Answer

Check if object is destroyed on level load, if so instantiate prefab? 1 Answer

Trouble with destroying an instantiated prefab 2 Answers

Issues with destroying a game object 2 Answers

Can't remove instantiated prefab 0 Answers


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