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 Gekigengar · Nov 21, 2013 at 07:22 AM · colormaterialschildchildrengroups

How to change material color to a specific object group's childs..?

This is the script I am currently Using.

I am trying to change all the child of the object group's color on a specific material.

This is what I have.

 public Transform target;
     
 public Texture2D colorPicker;
 private Texture2D styleTexture;
 
 public int textureWidth = 100;
 public int textureHeight = 100;
 public bool useDefinedSize = false;
     
 public int positionLeft = 0;
 public int positionTop = 0;
 public bool useDefinedPosition = false;
 
 public Color setColor;
 
 public bool showPicker = false;
 
 void Awake() {
     //If size is not using defined size, resize to Texture Size.
     if (!useDefinedSize) {
         textureWidth = colorPicker.width;
         textureHeight = colorPicker.height;
     }
     //If position is not using defined position, relocate to center of screen.
     if (!useDefinedPosition) {
         positionLeft = (Screen.width / 2) - (textureWidth / 2);
         positionTop = (Screen.height / 2) - (textureHeight / 2);
     }
     //Set up color preview box.
     styleTexture = new Texture2D(1, 1);
     styleTexture.SetPixel(0, 0, setColor);
 }
 
 void OnGUI(){
     //If picker is disabled, early return.
     if (!showPicker)return;
         //Color canvas.
         if (GUI.RepeatButton(new Rect(positionLeft, positionTop, textureWidth, textureHeight), colorPicker)) {
             //Get Mouse Position.
             Vector2 pickpos = Event.current.mousePosition;
             int canvasX = Convert.ToInt32(pickpos.x);
             int canvasY = Convert.ToInt32(pickpos.y);
             //Reposition Mouse input according to size and position of the canvas.
             Color col = colorPicker.GetPixel(((canvasX-positionLeft)*((colorPicker.width/textureWidth))),(colorPicker.height-((canvasY-positionTop)*(colorPicker.height/textureHeight))));
             //Return picked color.
             Debug.Log(col);
             //Set Color for display.
             setColor = col;
             }
         //Apply button.
         if (GUI.Button(new Rect(positionLeft + textureWidth - 60, positionTop + textureHeight + 10, 60, 25), "Apply")) {
             //Change material color of all child in the target object.
             foreach(Renderer r in target.parent.GetComponentsInChildren()) {
                 foreach(Material M in r.materials) {
                 M.color = setColor;
                     }
             }
             //target.renderer.material.color = setColor;
             // hide picker
             showPicker = false;
         }
             //Create Style for color preview.
             GUIStyle style = new GUIStyle();
             //Update Color for color preview.
             styleTexture.SetPixel(0, 0, setColor);
             styleTexture.Apply();
             style.normal.background = styleTexture;
             GUI.Box(new Rect(positionLeft + textureWidth + 10, positionTop + textureHeight + 10, 30, 30), new GUIContent(""), style);
         }
 }

I get this error :

 Assets/Scripts/ColorPicker.cs(59,61): error CS0411: The type arguments for method `UnityEngine.Component.GetComponentsInChildren<T>()' cannot be inferred from the usage. Try specifying the type arguments explicitly

How do I fix this..? Can anyone point out my mistakes?

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 Huacanacha · Nov 21, 2013 at 07:25 AM 0
Share

If the problem is on this line:

 foreach(Renderer r in target.parent.GetComponentsInChildren()) {

It's because you aren't specifying the type and it can't be inferred. Try specifying the type of the component explicitly:

 foreach(Renderer r in target.parent.GetComponentsInChildren<Renderer>()) {
avatar image Gekigengar · Nov 21, 2013 at 07:56 AM 0
Share

Thanks for the quick-answer!

Its working very fine..! :D

I hope you have a nice day sir!

But how do I make it work only for a certain material?

I wanted it to only work for material #1 for example.

1 Reply

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

Answer by robhuhn · Nov 21, 2013 at 07:24 AM

You need to specify that you want a renderer component back:

 target.parent.GetComponentsInChildren<Renderer>()
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

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

FindChild/Change color 1 Answer

Can't get children/Color not part of material 1 Answer

How to change color to every specific material in a child of a group..? 1 Answer

Set Specific Child's Material 2 Answers

One fraction of game object one material, other fraction different material? 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