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
1
Question by Steven-1 · Dec 27, 2010 at 01:38 PM · destroychilddeletespecific

finding and destroying specific children

I've tried :

var Shadows = GetComponentsInChildren(Projector);
for (s in Shadows)  Destroy(s.gameObject);

but it seems to destroy more than just the child, and the whole game seems to go nuts.

then I've tried :

var Shadows = GetComponentsInChildren(Projector);
for (s in Shadows)  Destroy(s);

which works, but ofcourse it only destroys the component, which leaves me with some transform-objects (which is memory leak)

then I tried:

for (var child:Transform in transform) 
{
    if (child.name == "shadowcast")
    {
        Destroy(child);//.gameObject);
    }
}

but that makes it crash as soon as it finds something with that name (infinite loop it seems)

what am I doing wrong ?

EDIT: now I've tried this:

var todelete : Array = new Array(); for (var child:Transform in transform) { if (child.name == "shadowcast") { print("destroyed shadow: "+child); //Destroy(child); todelete.Push(child.gameObject); } }

 for (var s in todelete) Destroy(s);

This in case destroying the child causes the for loop to be corrupted.

and now I get "MissingReferenceException: The object of type 'Transform' has been destroyed but you are still trying to access it." which refers to other children of the parent of these projectors. So it clearly deleted too much and lots of problems along with it. (on a side note: what I'm deleting is something (a projector) I don't acces at all in any of my scripts)

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 Mike 3 · Dec 27, 2010 at 03:41 PM

The first one finds projectors that are on the current gameobject, not just the children. You could do if (s.gameObject != gameObject) Destroy(s.gameObject);

The last one isn't destroying immediately, you'd need to use DestroyImmediate instead of Destroy for that to work

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 Steven-1 · Dec 27, 2010 at 04:59 PM 0
Share

for the first: the parent doesn't have a projector, so that shouldn't be a problem. last one: even if it doesn't destroy immediately "for (var child:Transform in transform)" should still only get to each child once. (I'm not using find("shadowcast") for exactly that reason)

avatar image Mike 3 · Dec 27, 2010 at 07:47 PM 0
Share

Oh, sorry, misread the latter one. Destroy(child) in that case will try destroy the transform, which shouldn't do anything at all. Not sure why it'd crash, but it needs to be Destroy(child.gameObject) in any case

avatar image Steven-1 · Dec 27, 2010 at 08:10 PM 0
Share

it seems as though destroying child.gameObject causes the parent to be destroyed as well. I really don't get why it's like this :S

avatar image Mike 3 · Dec 28, 2010 at 04:44 AM 0
Share

Are you absoloutely sure it destroys the parent? Have you checked the parent vanishes in the inspector?

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

No one has followed this question yet.

Related Questions

delete parent or send message to parent? 2 Answers

Destroy specific child object 1 Answer

how do you delete variables or arrays? 3 Answers

How to destroy GameObject when clicked on. 2 Answers

Detect when GameObject has been deleted / removed from scene 10 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