Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 hawken-2 · Sep 19, 2011 at 03:25 AM · childtaglayer

change layer of child

Does anyone know how to access the layer of a child and dynamically change it?

The code I have right now only changes the parent, and not the child. It tags the children well enough, so I may single them out for layer change.

 gameObject.tag = "myTag";
 
 for (var child : Transform in transform) {
     child.tag = "myTag";
 }
 
 var moveLayer : GameObject;
 moveLayer = gameObject.FindWithTag("myTag");
 moveLayer.layer = 11;

I tried the following, but layers are not part of transform.

 for (var childl : Transform in transform) {
     childl.layer = 11;
 }

How would one access them?

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

5 Replies

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

Answer by syclamoth · Sep 19, 2011 at 04:16 AM

To change the layer of a child transform's gameObject, use

 child.gameObject.layer = LayerMask.NameToLayer("WhateverYouCalledIt");

I should warn you, however, that iterating through a Transform like this only returns the first layer of children- it doesn't give you any children that those children might have. If you want to change an entire hierarchy, you should write some kind of recursive function which can be called on any given Transform to change the layers all the way down, like this (apologies in advance for dodgy JS syntax- I usually use C#):

 function ChangeLayersRecursively(var trans : Transform, var name : String)
 {
     for (var child : Transform in trans)
     {
         child.gameObject.layer = LayerMask.NameToLayer(name);
         ChangeLayersRecursively(child, name);
     }
 }

This will make a deep search of a Transform hierarchy and change every child in it instead of just the first layer.

Comment
Add comment · Show 3 · 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 hawken-2 · Sep 19, 2011 at 08:19 AM 0
Share

Thanks for this, I was racking my brains on how to target child layers, this is it. The second bit of code I wasn't able to get running, I'm also not sure what name is doing, does "String" need to be replaced?

Anyhow, I can make move of the grandchild objects into children and use the first bit of code just fine, thanks again.

avatar image vitaly · Nov 20, 2013 at 04:24 AM 2
Share

The working example for C# which also changes the layer of a transform itself:

 public static void ChangeLayersRecursively(this Transform trans, string name)
 {
     trans.gameObject.layer = Layer$$anonymous$$ask.NameToLayer(name);
     foreach(Transform child in trans)
     {            
         child.ChangeLayersRecursively(name);
     }
 }
avatar image XcentY · Jul 11, 2015 at 02:48 PM 0
Share

vitaly. Do you have a working example of what you said ? When I said it was wrong, you replied it is not wrong, it uses extension $$anonymous$$ethods. I want to see a working example of your call "child.ChangeLayersRecursively(name)" within the foreach loop of your declared $$anonymous$$ethod "ChangeLayersRecursively(this Transform trans, string name)"

Or you declared another method elsewhere that you can apply to a transform object.

This can explain your convenient answer "it uses extension $$anonymous$$ethods".

However what you give as a "working example" will only work if you give where the method "ChangeLayersRecursively(name)" comes from.

In the actual state , the use of this method return "the method ChangeLayersRecursively is not a member of the Transform object."

So Please provide the full working example with your Extension class that I can see how you achieved this :)

And I say that because I used your code as part of my program and your code only work if I put "ChangeLayersRecursively(child, name)" in the foreach loop ins$$anonymous$$d of an unknow method of a Transform Object.

avatar image
7

Answer by YasinJavaid_ · Mar 05, 2019 at 07:37 AM

Hello simple way to change layer in all child

 // obj is your GameObject change it with your
     foreach (Transform child in obj.GetComponentsInChildren<Transform>(true))  
      {
                child.gameObject.layer = LayerMask.NameToLayer ("Default");  // add any layer you want. 
      }



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 XcentY · Oct 07, 2014 at 01:39 PM

the comment of vitaly is wrong. child.ChangeLayersRecursively(name); the method ChangeLayersRecursively is not a member of the Transform object. ChangeLayersRecursively(child, name); is better

Comment
Add comment · Show 3 · 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 vitaly · Oct 07, 2014 at 01:45 PM 0
Share

It is not wrong. It uses Extension $$anonymous$$ethods, the feature of C# http://msdn.microsoft.com/en-us/library/vstudio/bb383977(v=vs.110).aspx

avatar image lodendsg · Jul 11, 2015 at 12:11 PM 0
Share

Depends on where you add the code for us we add this to a static class as opposed to extending. We also changed this take a GameObject and gave it an overload to pass layer as string or int.

Find our adjustments below might be useful for someone.

 public static void ChangeLayers(GameObject go, string name)
 {
     ChangeLayers(go, Layer$$anonymous$$ask.NameToLayer(name));
 }
 
 public static void ChangeLayers(GameObject go, int layer)
 {
     go.layer = layer;
     foreach (Transform child in go.transform)
     {
         ChangeLayers(child.gameObject, layer);
     }
 }
avatar image batpox · Jul 12, 2017 at 11:27 AM 1
Share

@XcentY: Just to be clear for anyone reading this post; Vitaly's answer is not wrong, and your answer is not "better". His is simply different than yours. Extensions are a great feature of C#. Check it out ;)

avatar image
0

Answer by keshav9999 · Nov 24, 2019 at 06:53 PM

This worked for me

foreach (var partChild in part.part.GetComponentsInChildren()) { partChild.gameObject.layer = 13;//layer number here },This worked fo me.

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 Eric5114 · Nov 25, 2019 at 07:54 AM

Do you have a working example of what you said ? When I said it was wrong, you replied it is not wrong, it uses extension Methods. I want to see a working example of your call "child.ChangeLayersRecursively(name)" within the foreach loop of your declared Method "ChangeLayersRecursively(this Transform trans, string name)"

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

8 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to change layer of Gameobjects with same tags? 1 Answer

set animation attributes for certain objects 1 Answer

How to get the childrens tag from the parent GameObject? 1 Answer

Get object child list with tag 2 Answers

How can I get the tag of a child object? 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