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 all with Player and Camers 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 · 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 Seth-Bergman · Nov 01, 2012 at 02:53 PM 0
Share

I re-formatted your code.. next time try using the 010101 button (with your code selected)

2 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by Seth-Bergman · Nov 01, 2012 at 03:02 PM

OK, I think I see what you are after..

maybe like this?

 var agoAllGameObjects:GameObject[] = FindObjectsOfType(GameObject) as GameObject[];
 for(iGO in agoAllGameObjects)
 {
    if(!iGO.GetComponent(Camera))
       Destroy(iGO);     
 }
Comment
Add comment · Show 1 · 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 Gabo_campitelli · Nov 01, 2012 at 04:26 PM 0
Share

That should do it

avatar image
0

Answer by Gabo_campitelli · Nov 01, 2012 at 03:58 PM

I can think of two ways, first for all gameobjects you get with FindObjectsOfType(GameObject) do an if asking if getComponent("Player") OR getComponent("camera") are null. If null destroy. Another way is to use tag. Same as above but in each if you ask for the tag. if the tag is NOT player or camera (or maincamera) destroy. and also if you want to do this to load a new level and keep your player and camera setup you can just add an atribute to the gameobjects via script called DontDestroyOnLoad http://docs.unity3d.com/Documentation/ScriptReference/Object.DontDestroyOnLoad.html this way the objects that have that are not destroyed when changing levels.

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

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

12 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

Related Questions

Destroy all GameObjects EXCEPT some... 2 Answers

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

When I destroy my gameObject my script Stops on working 3 Answers

How to order newly created Objects? 1 Answer

Automatically uncheck? 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