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 saotele2 · Nov 01, 2012 at 02:13 PM · gameobjectdestroyenumerate

Destroy all GameObjects EXCEPT some...

I need to Destroy all GameObjects on current scene, EXCEPT Player and Camera classes. How can I do it?

 var agoAllGameObjects:GameObject[] = FindObjectsOfType(GameObject);
 for(var iGO:int = 0; iGO < agoAllGameObjects.length; iGO++)
 {
    agoAllGameObjects[iGO].GetType();       //always GameObject :(
    agoAllGameObjects[iGO].GetType("Name"); //always GameObject :(
    if(agoAllGameObjects[iGO].GetType() != Camera)
       Destroy(agoAllGameObjects[iGO]);     //Destroys EVERYTHING =((((
 }
 
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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by bdjnk · May 14, 2013 at 07:45 AM

All GameObjects are of type GameObject. What distinguishes them is various attributes and components. For example I could say

 if(agoAllGameObjects[iGO].name != "Camera")

or perhaps

 if(agoAllGameObjects[iGO].GetComponent(Camera) == null)

but GetType on a GameObject will always return GameObject, because that IS its type.

Comment
Add comment · 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
0

Answer by adrenak · Nov 01, 2012 at 03:36 PM

The agoAllGameObjects itself is an arary of GameObject, so its only obvious that .GetType will always be a GameObject. To destroy everything except the player and the camera, what you can do is check for tag or name. So, inside the for loop, have these statements instead of the above ones :

 if(agoAllGameObjects[iGO].tag != "Player" && agoAllGameObjects[iGO].tag != "Camera"){
     Destroy(agoAllGameObjects[iGO]);
 }


The above line assume that the player carries a tag named "Player" and the camera(s) have a tag called "Camera". It should work properly.

Hope this helps.

-Vatsal

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 saotele2 · Nov 02, 2012 at 08:00 AM 0
Share

The solution is obvious with TAGs, I should fix the problem WITHOUT using any TAG!

avatar image bahisht2k2 saotele2 · Dec 23, 2018 at 05:27 AM 0
Share

dear how have find the solution. i also need help in this regard

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

13 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

Related Questions

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

Destroy all GameObjects EXCEPT some... 2 Answers

How to Destroy a gameobject on collision 3 Answers

How to delay lines of code. 2 Answers

Destruct gameobject by call from another script 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