• 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 sanks007 · Jun 29, 2013 at 11:45 AM · prefabcolorrenderer

Changing color of Children of instantiated prefab on click of other gameobject

Hello guys. I am trying to change the color of children of Instantiated prefab to the color of gameobject which is clicked. Suppose prefab1 contains gameobject2,gameobject3 which are spheres and gameobject4 which is a clyinder. GameObject1 which again is a sphere(red colored) when clicked should instantiate the prefab and color of gameobject2,3,4 should be red colored. I am able to instantiate the prefab on click of the Gameobject1 using raycasting so what is supposed to be done next ? Tried something like this:

GameObject go = (GameObject)Instantiate(Resources.Load("Gameobject")); go.name="first"; GameObject.Find("first").renderer.material.color=col1;

Comment
Add comment · Show 4
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 Em3rgency · Jun 29, 2013 at 01:33 PM 0
Share

I don't know about the changing material color bit (I'd just use a different material altogether) but you can simplify your code.

 GameObject go = (GameObject)Instantiate(Resources.Load("Gameobject")); 
 
 go.renderer.material.color=col1;
avatar image sanks007 · Jun 29, 2013 at 01:46 PM 0
Share

Tried that as well nothing works..The object gets instantiate but the color is not changing.how is it possible to get the child elements of the prefab ? If i get the child elements i can then set the color individually.!

avatar image moha · Jun 29, 2013 at 03:54 PM 0
Share

try getComponentInChildren().material.color = myColor

avatar image sanks007 · Jul 01, 2013 at 05:08 AM 0
Share

@moha i tried this but it gives me the following error..

The type arguments for method 'UnityEngine.GameObject.GetComponentInChildren()' cannot be inferred from the usage.Try Specifying the type arguments explicitly.

and the this is what i tried.

GameObject go = (GameObject)Instantiate(Resources.Load("1ValenceBondElementPrefab"));

go.GetComponentInChildren().material.color=col1;

1 Reply

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

Answer by Immanuel-Scholz · Jul 01, 2013 at 06:51 AM

GetComponentsInChildren will give you an array of all components of this type within all direct children of the game object you call it on.

So first you have to make sure you can find your prefab instance, e.g. lets say by giving it the name "prefab" and use GameObject.Find("prefab"). (Other ways would include using Tags or assigning it to a public variable in the editor inspector).

so it should be in a for loop - something like this (C#):

 var prefab = GameObject.Find("prefab"); // prefab instance must be named "prefab"
 var first = ...; // instantiate your "first" object with the red sphere
 var col1 = first.renderer.material.color; // get its color
 foreach (var child in first.GetComponentsInChildren<Renderer>())
     child.material.color = col1;
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 sanks007 · Jul 01, 2013 at 07:21 AM 0
Share

Thanks bro... though most of the part was working i used ur foreach loop and it worked properly.. and surely saved lot of time and brains...

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

18 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

Related Questions

Changing two different objects renderer colour 1 Answer

How to redner a single texture, multiple colors without increasing draw calls. 1 Answer

Dynamically fill color in a gameobject? 1 Answer

Fade out a material that has a texture 1 Answer

change the colour of mass placed tree leaves (or make them transparent) 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