• 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 Serotonina · Dec 02, 2021 at 08:14 AM · listdestroydestroygameobjectclones

Destroying clone object in list does not remove the first one

Hi everyone,

I'm currently developing a system where Gameobjects are instantiated as Clones and added to a list. After the player makes a selection (each gameobject have a button attached to it) the onClick event will fire several Invokes and also remove the Gameobjects from the list and destroy the clones.

The issues I'm facing is that whenever the player has more than one Button to choose from two scenarios happens:

First scenario: player interact with the first button, all the Clones are destroyed and everyt$$anonymous$$ng goes according to plan. Second scenario: player interact with the second (or any following) button, and all the Clones are destroyed, minus the first one.

I've tried several ways (Foreach loop, positive For loop, negative For loop) but not$$anonymous$$ng seems to work.

Here are the two main parts that regulates t$$anonymous$$s code.

Instantiating the Objects and calling the Method:

 public void ShowResponses(ui_Dialogue_Response[] ui_DR)
 {
         float responseBoxWidth = 0;
 
         for (int i = 0; i < ui_DR.Length; i ++)
         {
             ui_Dialogue_Response Ui_D_R = ui_DR[i]; //T$$anonymous$$s is a class that holds the various aspects of the response (sprite, name etc.)
             int responseIndex = i;
 
             GameObject responseButton = Instantiate(emojiResponseButton.gameObject, emojiContainer);
             Button responseButtonButton = responseButton.GetComponent<Button>();
             Sprite responseButtonSprite = responseButton.GetComponent<Sprite>();
 
             responseButton.gameObject.SetActive(true);
             responseButton.GetComponent<Image>().sprite = Ui_D_R.ResponseEmoji.sprite;
             responseButton.GetComponent<Button>().onClick.AddListener(() => OnPickedResponse(Ui_D_R, responseIndex)); //T$$anonymous$$s calls the method that will destroy the buttons and do some other stuff
 
             tempResponseEmoji.Add(responseButton);
 
             responseBoxWidth += emojiResponseButton.sizeDelta.x;
         }
 }

The method:

 private void OnPickedResponse(ui_Dialogue_Response Ui_D_R, int responseIndex)
     {
         foreach (GameObject emoji in tempResponseEmoji)
         {
             Destroy(emoji);
         }

          tempResponseEmoji.Clear();

         /*
         for (int i = tempResponseEmoji.Count -1; i >=0 ; i--)
         {
             GameObject emojiClone = tempResponseEmoji[i];
             Destroy(emojiClone);
             //Destroy(tempResponseEmoji[i].gameObject, 0.1f);
             tempResponseEmoji.RemoveAt(i);
             
         }*/
 }

As you can see I've tried a couple of ways to remove or destroy those gameobjects, but they do not work.

What am I doing 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 CodesCove · Dec 02, 2021 at 07:54 PM 0
Share

So you are cloning from scene game object and not instantiating from prefab? Are you sure that the 1st button you are pressing is not the "original" one? If so, then it will not be in the list and will not be destroyed.. of course this means that you must have some predefined button events to make the "original" 1st button work at all..

0 Replies

· Add your reply
  • Sort: 

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

138 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

Related Questions

A node in a childnode? 1 Answer

Remove a destroy object from a list, then reload the list 2 Answers

Script doesn't work anymore! 2 Answers

Destroy() not working on prefab instance 2 Answers

How can I debug gameObject destruction? 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