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 09:33 PM by KnottyDreadLox for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by KnottyDreadLox · Jan 28, 2018 at 09:05 AM · materialgetcomponentrenderertagchildren

Help with script , Change tag depending on material attached

Hi all , i have this problem that is making me itch my head. I have a grid of tiles , and instead of changing their tags manually, i want it to be possible for me to add a script and have the the tiles change their own tag based on what material colour i attach to them.

The Materials im using look like this , Each having a default "UI" shader (The reason i used the Default UI shader is because its the only one that brings out the colour without needing a light source. If this is wrong or a better way , please correct me)

alt text

Now , here is my hierarchy alt text

Lastly , this is my Code , Which is attached to "TileGrid"

     public Material Red;
     public Material Yellow;
     public Material Blue;
 
     void Start()
     {
         Transform TileGrid = transform;                             
         int GridChildCount = TileGrid.childCount;                   
 
         for (int i = 0; i < GridChildCount; ++i)                    
         {
             Transform GridRow = TileGrid.GetChild(i);              
             int setChildCount = GridRow.childCount;                 
 
             for (int j = 0; j < setChildCount; ++j)                   
             {
                 SetTagAccordingToMaterial(GridRow.GetChild(j));    
             }
         }
     }
 
     private void SetTagAccordingToMaterial(Transform Tile)
     {
         Renderer TileRenderer = Tile.GetComponent<Renderer>();
 
         if (TileRenderer.material == Red)
         {
             tag = "Red";
         }
         else if (TileRenderer.material == Yellow)
         {
             tag = "Yellow";
         }
 
         else if (TileRenderer.material == Blue)
         {
             tag = "Blue";
         }
     }
 }

So to give an idea of what the code above is meant to do, remembering that this script is attached to "TileGrid" , is when I click run first get the material of the Children's Children (so basically the "Tile"). Than to change the Tag of "Tile" depending on the attached material.

Also , I drag all 3 materials/colours into the "public Material Red/Yellow/Blue" in Unity.

So what's happening ?

Basically Nothing , Whenever i run , the Tags do not change.

I'm wondering if maybe the material i am using isn't recognized by the GetCompontent ?

Any Help would be gladly appreciate it , Im also not 100% sure if my Loops to get the rendered in the first place work properly as i haven't been able to test it since nothing is working.

colours.png (1.6 kB)
grid.png (1.8 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

Answer by yummy81 · Jan 28, 2018 at 01:55 PM

There are two problems with your SetTagAccordingToMaterial method:

  1. The condition :

      TileRenderer.material == Red
    

does not work. It works when you compare names.

 TileRenderer.material.name == Red.name + " (Instance)"
  1. In the statement

      tag = "Red"
    

tag refers to your TileGrid's tag, but what you want is each Tile's tag. So change it to:

 Tile.tag = "Red"

And the same for the other two colors. Of course, don't forget to add those three tags to Project Settings->Tags and Layers

     private void SetTagAccordingToMaterial(Transform Tile)
     {
         Renderer TileRenderer = Tile.GetComponent<Renderer>();
     
         if (TileRenderer.material.name == Red.name + " (Instance)")
         {
             Tile.tag = "Red";
         }
         else if (TileRenderer.material.name == Yellow.name + " (Instance)")
         {
             Tile.tag = "Yellow";
         }
         else if (TileRenderer.material.name == Blue.name + " (Instance)")
         {
             Tile.tag = "Blue";
         }
     }
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 KnottyDreadLox · Jan 28, 2018 at 09:07 PM 0
Share

Thank you so much !

You have no idea how thankful I am right now ! it worked

Follow this Question

Answers Answers and Comments

136 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 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

Gameobject Renderer not working. 1 Answer

Issue where Fading out Game Object causes screen to blank and then reload with gameobject gone 0 Answers

Material disappears after LoadScene 1 Answer

Rotating and lerping to another material with custom shader. 0 Answers

Tag control 0 Answers


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