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 azmundai · Jul 06, 2013 at 07:04 AM · editorgameobjectsselection

Editor Script to replace objects (problem with selection)

This is what I have sofar, and it works, but on a game object with 23 children, only 11 of them get changed.

 using UnityEngine;
 using System.Collections;
 using UnityEditor;
 using System.Collections.Generic;
 
 public class CreateHolders : MonoBehaviour {
     
     [MenuItem ("Pluto/Place Holders")]
     static void PlaceHolders () {
         foreach(GameObject go in Selection.gameObjects){
             foreach (Transform child in go.transform) {
                 if(child.tag == "Wall"){
                     GameObject tempGo = new GameObject(child.name.Replace("TS001_", "WALL"));
                     tempGo.tag = "WallHolder";
                     tempGo.transform.parent = Selection.activeTransform;
                     DestroyImmediate(child.gameObject);
                 }
             }
         }
 
     }
 }

EDIT1: It appears to only do half of the children First one does 11/23, leaving 12. Second one does 6/12 leaving 6. Third time does 3/6 leaving 3. etc .. strange?

EDIT2: I tried unparenting the items I wanted to destroy and not destroying them, and it still only processed 1/2 of the children. Problem doesn't appear to be with DestroyImmediate().

Comment
Add comment · Show 2
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 supercouge · Jul 06, 2013 at 07:48 AM 0
Share

It is recommended to use Destroy ins$$anonymous$$d of DestroyImmediate. It should also solve your problem.

Edit : Sorry, I forgot that you were in edit mode :/

avatar image azmundai · Jul 06, 2013 at 08:03 AM 0
Share

yea i tried that. doesn't seem to work:

Destroy may not be called from edit mode! Use DestroyImmediate ins$$anonymous$$d. Also think twice if you really want to destroy something in edit mode. Since this will destroy objects permanently. UnityEngine.Object:Destroy(Object) CreateHolders:PlaceHolders() (at Assets/Editor/CreateHolders.cs:19)

Also, see EDIT2

1 Reply

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

Answer by supercouge · Jul 06, 2013 at 08:20 AM

I tried that and it seems to work.

         foreach(GameObject go in Selection.gameObjects){
             for(int i = 0; i < go.transform.GetChildCount(); i++) {
                 DestroyImmediate(go.transform.GetChild(i).gameObject);
                 i--;
             }
         }
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 azmundai · Jul 06, 2013 at 08:28 AM 0
Share

awesome! thanks!!

avatar image Jamora · Jul 06, 2013 at 08:36 AM 0
Share

I'm such a slow typist... Well, as to why this works, it is because you were destroying what you were iterating through, so you skipped every other index. This usually gives a runtime error when done with foreach... Ins$$anonymous$$d of decreasing one from i you could also for (int i = 0; i< go.transform.childCount; ) or while(go.transform.childCount>0)

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

19 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

Related Questions

Auto-Selecting something else in Scene-View? 0 Answers

How can I set a selection-sphere in the editor 1 Answer

Preventing editor deselect on MacOS 1 Answer

Editor selection order 2 Answers

Support multiple selection with OnSceneGUI handles (The targets array should not be used inside OnSceneGUI) 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