Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 elfasito · Mar 20, 2021 at 02:47 PM · instantiatelistnewbiecloned

How to add & remove specific gameobject from a list and instantiate them

Hello People.

Im trying to add gameobject to a list in runtime and later remove a desired gameobject from it. at the same time I add gameobjects to the list I need to instantiate them too, to show in a scroll view. and later destroy the desired instance from a button.

There is my attemp for now:

     public Transform ContentContainer; //used to set where the instantiated objects are added.
     public List<GameObject> Objects3dCodigo = new List<GameObject>();
     public GameObject AddObject;
     private GameObject Clone;
     private void Start()
     {
         //Fill();
     }
 
     public void Fill()
     {
         Objects3dCodigo.Add(AddObject); //add gameobject to list
         for (int i = 0; i < Objects3dCodigo.Count; i++) //instantiate them
         {
             Clone = Instantiate(Objects3dCodigo[i], ContentContainer.transform); 
         }
     }
 
     public void RemoveObjects()
     {
         for (int i = 0; i < Objects3dCodigo.Count; i--)
         {
             Objects3dCodigo.RemoveAt(i);
             Destroy(Clone.gameObject);
         }
     }

My problems so far:

1- each time I add a new gameobject to the list, the instantiate function multiply the cloned items.

2- when I call RemoveObjects(), only remove the last instance.

Can give me some guidance?.

alt text

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
1
Best Answer

Answer by mangosauce_ · Mar 20, 2021 at 03:14 PM

I made the appropriate fixes to your code and added some comments to help you understand what's happening. Essentially, you were adding the gameObject prefab (AddObject) to the list, then saving only the most recently instantiated clone of that prefab to Clone. Then, when you were looping through the list in RemoveObject, you were removing the prefab from the list, and destroying the most recently instantiated Clone.


In my revised version of the code, you are instantiating a clone of AddObject, then adding the clone to the list. Then in RemoveObject, you enumerate through the list, removing the clones from the list and destroying them. Hope the code helps explain things a little!


         public Transform ContentContainer;
         public List<GameObject> Objects3dCodigo = new List<GameObject>();
         public GameObject AddObject;
 
         private void Start()
         {
             // Adds a new gameObject of type
             // AddObject to the Objects3dCodigo list
             Fill();
         }
 
         public void Fill()
         {
             // Moved Clone variable to be local to the method
             GameObject Clone = Instantiate(AddObject, ContentContainer);
             // We just instantiated a gameObject of type AddObject,
             // then saved its value to Clone. Now we will add the object
             // we just saved to the list of objects.
             Objects3dCodigo.Add(Clone);
         }
 
         public void RemoveObjects()
         {
             // In order to loop through a list backwards, we
             // need to start at the list's length - 1, then
             // continue ticking down until we reach 0.
             for (int i = Objects3dCodigo.Count - 1; i >= 0; i--)
             {
                 // Save the object at position i in the list
                 // to the local variable Clone
                 GameObject Clone = Objects3dCodigo[i];
                 // Remove the object from the list
                 Objects3dCodigo.RemoveAt(i);
                 // Destroy the object we got from the list
                 Destroy(Clone);
             }
         }



Please let me know if this helped any! :)

Comment
Add comment · Show 4 · 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 elfasito · Mar 20, 2021 at 04:12 PM 1
Share

Hi @mangosauce_ Thanks for the script corrections with the detailed explanations, I appreciate it. The instantiating now it's working pretty well.

but when I call the function to remove objects, it remove all instances and items from the list at once.

maybe its because im calling the function RemoveObjects() from the wrong gameobject? Im calling it from the "$$anonymous$$" button added in the clone instance (as you can see in the screenshot).

PD: I uncomment the Fill(), in the void start because I add the gameobjects to the list in runtime, in the start need to be empty.

avatar image mangosauce_ elfasito · Mar 20, 2021 at 04:27 PM 1
Share

I see! In that case it's a little more complicated. You need some way of identifying which GameObject you want to destroy when you press the button. Then, using this new RemoveObject method, pass that GameObject as an argument. The method will check if your list contains the object to destroy, then remove/destroy it.


         public void RemoveObject(GameObject o)
         {
             // Check if the list contains the gameObject
             // we want to destroy
             if (Objects3dCodigo.Contains(o))
             {
                 // Remove the object from the list
                 Objects3d.Codigo.Remove(o);
                 // Destroy the object
                 Destroy(o);
             }
         }



If you're assigning GameObjects to their respective Delete buttons, it should be easy to add a listener to the OnClick event of that button. I found a super quick video that might give you an idea on how to do this.

avatar image elfasito mangosauce_ · Mar 20, 2021 at 05:24 PM 1
Share

Thanks, Its working now with the new method. I need to make some changes to adequate it to my needs, but the method works. thanks for your help and time.

avatar image karnas01 · Nov 10, 2021 at 08:26 AM 0
Share

Thank you a lot! It work for me!

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

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

A node in a childnode? 1 Answer

Destroying the cloned gameObject 2 Answers

cant get characters to load 1 Answer

Instantiate random from a list gives error: "Reference not set to an instance" 1 Answer

Spawning 2 objects in exactly the same place!!! 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