Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by Detinator10 · Jun 12, 2016 at 05:41 PM · gameobjectlistdestroycards

How to add a Gameobject to a list and remove it from Game World?

I want to add cards (that are already instantiated) to my discard pile list, but then also remove them from the Game World so that they only exist as part of the list. I've tried:

         discard.Add(card);
         DestroyImmediate(card);

and it doesn't work, it adds the card to the list and then destroys the object, so the list is empty. How would I go about doing this

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by gribbly · Jun 12, 2016 at 06:02 PM

What are you trying to achieve? What does the discard pile list do?

Would it solve your problem to add a copy of the card to the discard list?

 GameObject cardCopy = card;
 discard.Add(cardCopy);
 DestroyImmediate(card);
Comment
Add comment · Show 5 · 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 Detinator10 · Jun 12, 2016 at 06:08 PM 0
Share

What im trying to accomplish is to add gameobjects to a list, yet not have them actually exist in the world until I instantiate them. Once I've instantiated them, I want to move them to another list and remove them from the game world so they exist solely in the list and not in the hierarchy. I already tried making a copy, and it doesn't seem to work. I still get the exact same result which is when i destroy the card, I get an empty spot in my discard list and it still is in the game world. If it changes anything, the card variable is static.

avatar image Detinator10 · Jun 12, 2016 at 06:10 PM 0
Share

the discard pile list serves as a discard pile basically, I have to have one for this particular game (can't just reshuffle the deck) because cards will be added to and taken away from the deck. So i need to keep track of what cards still exist but have been played

avatar image TonicMind · Jun 12, 2016 at 06:37 PM 0
Share

Well ins$$anonymous$$d of Instantiating and destroying your objects you ought to look in to object pooling. There's a tutorial on this website for that, but the basic premise is that you call yourGameObject.setActive(false); whenever you want it deactivated (disappear, not visible/functioning for the time being), and yourGameObject.setActive(true) when you want it visible.

The combination of Instantiate() and any Destroy() method is extremely inefficient to be doing every game loop or even most of the time. It will kill your frame rate. Object pooling is the way to go, and its actually more manageable. You won't have to check for null references all the time because whatever you need will already exist.

Also when you call setActive() on anything, the thing still exists. It doesn't disappear from memory, but it does disappear from the game environment (from the players perspective). Using this and object pooling you could keep your List of gameobjects and still have them disappear from the game.

avatar image TonicMind TonicMind · Jun 12, 2016 at 06:39 PM 0
Share

Additionally; you can create functions like so:

public void OnEnable() { //Do stuff when enabled } //AND public void OnDisable() { //Do stuff when DISabled }

Which are called by the unity engine whenever the game object is enabled or disabled, respectively. Its really nifty.

avatar image Detinator10 TonicMind · Jun 13, 2016 at 04:14 AM 0
Share

Thanks, I'm gonna try and implement this tomorrow, put your comment as an answer and I will mark it correct ;)

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

57 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 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 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

How can I destroy a Gameobject from a list at a specific index 1 Answer

Shake GameObject in 0 Answers

Destroy(GameObject) works after 3 attempts 0 Answers

No error code? Not sure where to go? 1 Answer

Another guy who doesnt get the Destroy function to work. 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