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 /
  • Help Room /
This question was closed Jan 28, 2018 at 08:47 AM by KnottyDreadLox for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by KnottyDreadLox · Dec 16, 2017 at 04:15 PM · materialslooptagschildrenheirarchy

Get material of Children's children and Change tag help

Hi all , Im having some trouble with my code.

Basically , i am trying to get my objects to change their own Tags bassed on their material attached. Their materials are solid colours (Red , blue , etc)

So far , i'm trying to use this code. Basically i have 1 object named pack that has a few sets. Each set has a few cubes as shown in the image . The code isn't working obviously because idk how to do this and this is me just trying to use whatever knowledge i have , hence why i need help :)

alt text

this is my code atm :

 Material Red;
 Material Yellow;
 Material Blue;
 // Have materials dragged into this on unity 

 Void Start() {

     int ChildrenCount = transform.childCount;    
     // Get all children (I think this is only counting my Sets)

     List<Material> MaterialChecker = new List<Material>();                  
     // Make new List for materials

     for (int i = 0; i < ChildrenCount; i++)                                                    
    // loop through children
     {
         GameObject children = transform.GetChild(i).gameObject;                         
         children.GetComponent<Renderer>().materials = MaterialChecker.ToArray();

                 if (children == Red)
                 {
     
                     tag = "Red";
                 }
     
                 if (children == Yellow)
                 {
     
                     tag = "Yellow";
                 }
     
                 if (children == Blue)
                 {
     
                     tag = "Blue";
                 }
 
 }
 

This Start script is attached to "Pack" because this script will have to check all the Cubes in all the sets.

All Cubes have a Default tag of "Cube" incase that helps.

The error i get is "set1 doesn't have a renderer attached.

So essentially i need a way to get this script to check the Child's Children's material and based on their material , change their tag accordingly

Could someone please help evaluate my code and help me out ? Thank you all ! I'm still somewhat new to programming so bare with my ignorance , i'm still learning :)

pic1.png (1.4 kB)
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

  • Sort: 
avatar image
0
Best Answer

Answer by Hellium · Dec 16, 2017 at 04:35 PM

Try this:

 public Material Red;
 public Material Yellow;
 public Material Blue;
 
 // Have materials dragged into this on unity 
 void Start()
 {
     Transform pack = transform ;
     int packChilCount = pack.childCount ;
     for( int i = 0 ; i < packChilCount ; ++i )
     {
         Transform set = pack.GetChild( i ) ;
         int setChildCount = set.childCount ;
         
         for( int j = 0 ; j < setChildCount ; ++j )
         {
             SetTagAccordingToMaterial( set.GetChild( j ) ) ;
         }
     }
 }
 
 private void SetTagAccordingToMaterial( Transform cube )
 {
     Renderer cubeRenderer = cube.GetComponent<Renderer>();
     if( cubeRenderer.material == Red ) tag = "Red" ;
     else if( cubeRenderer.material == Yellow ) tag = "Yellow" ;
     elseif( cubeRenderer.material == Blue ) tag = "Blue" ;    
 }
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 KnottyDreadLox · Dec 16, 2017 at 04:56 PM 0
Share

Hi thanks for replying @Hellium !

The Switch part gives me an error "A switch expression or case label must be a bool, char, string, integral, enum, or corresponding nullable type in C# 6 and earlier"

The cases give me "Constant value is expected"

Smart idea on the switch case though , i didn't think of that idea !

avatar image Hellium KnottyDreadLox · Dec 16, 2017 at 06:19 PM 0
Share

You will have to use if elseif in that case. Answer fixed.

Follow this Question

Answers Answers and Comments

123 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 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 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 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Unable to use tags on children of a gameobject. 0 Answers

Script to change GameObject materials based on tag? 2 Answers

Loop through children and their children 1 Answer

FindObjectsOfType not find all objects and code not do anything. 0 Answers

Coroutines Madness (); 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