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 TriggerHappyNed · Mar 31, 2014 at 01:14 AM · gameobjectdestroylists

How do I save a gameObject to a list so i can destroy in the scene?

I am trying to create an inventory system and when I add a weapon to the player, I remove the weapon from the inventory list and try to add the weapon that is currently equipped to the inventory list:

 if(port1.transform.childCount > 0)
 {
     GameObject obj = port1.transform.GetChild(0).gameObject;
     inventoryWeapons.Add(obj);
     Destroy( port1.transform.GetChild(0).gameObject);
 }
 GameObject weap = (GameObject) Instantiate(itemToEquipt);
 weap.transform.parent = port1.transform;
 weap.transform.localPosition = Vector3.zero;
 weap.transform.localRotation = Quaternion.identity;
 inventoryWeapons.Remove(itemToEquipt);
 itemToEquipt =null;
 

I get an error that the gameobject was destroyed and I'm still trying to access it. is there a way I can save an instance of the weapon previously attached without keeping it in the scene? the weapon gameobject has information I want to preserve (damage/accuracy/etc).

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 getyour411 · Mar 31, 2014 at 01:32 AM 0
Share

If your destroy the gameObject, then it is destroyed. Why destroy it and then see$$anonymous$$gly reinstantiate it?

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Bunny83 · Mar 31, 2014 at 02:26 AM

The answer is: No! You can't "store" a GameObject somewhere else then in the scene. The only exception are prefabs. They are instances but not in the scene, but prefabs can't be created at runtime. You have to keep the object in your scene. Just disable it with SetActive(false).

Comment
Add comment · Show 2 · 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 TriggerHappyNed · Mar 31, 2014 at 02:36 AM 0
Share

That's what I'm trying now, but what I want is to have 3(or more) weapon ports and have the weapon moved to the port selected.

I am right now creating the objects physically inside the "loot" object and then making a part of the player its new parent when picked up, then when equipped, the port become the parent.

haven't had time to test this system, but I can't see why it shouldn't work... right now.

avatar image Bunny83 · Mar 31, 2014 at 02:46 AM 0
Share

Sure, parenting the picked-up objects to the player is the usual way. You could also have an empty gameobject as child of your player object (lets call it "backpack") and parent all picked-up objects to this container object. That way you can simply deactivate the container and all childs are disabled as well. Also the objects don't clutter other active parts of the model.

Since you store a reference in your list you have easy access to the objects inside the "backpack".

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

21 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

using Contains(gameObject) to find and destroy a gameObject from a list 2 Answers

Destroy all Objects of a Type in Game 1 Answer

Trash Collection For Instantiated Game Object 1 Answer

How to Destroy a gameobject on collision 3 Answers

Only delete the gameobject ONCE 2 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