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 Jacobsc · Nov 29, 2019 at 08:16 PM · materialcolorchildchildrenchild object

FindChild/Change color

So I'm making a script to change the color of two parts of my player ( a 3 piece rocket ship). The body and front need to change color while the engine stays black. It needs to change the colors when it enters an object but I'm having problems calling the child and I'm not sure if they will change color.

public class Collection : MonoBehaviour { public Color White; public Color Green; public Color Red;

 public Transform tForm;
 public GameObject gObject;
 private gObject.tForm.Find.("Body") = body;


 void OnTriggerEnter(Collider other)
 {
     //green
     if (other.gameObject.tag == "GreenCube")
     {
         other.gameObject.SetActive(false);
         //gObject.tForm.Find("Body");
         //Body.material.SetColor("_Color", Green);

     }
     //red
     if (other.gameObject.tag == "RedCube")
     {
         other.gameObject.SetActive(false);
     }
 }

}

I've only started on the green, I'll do the Red once the answer is given.

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

· Add your reply
  • Sort: 
avatar image
-1

Answer by PruneDealer · Nov 29, 2019 at 09:55 PM

Well if the game object in this script is the body and the other parts are child objects you should be able to do something along the lines of this

 private Color childColor;
 
 public void ChangeColor()
 {
 childColor = new Color(1, 0, 0, 1) //red
 gameobject.FindCompenentsInChildren<Material.Color>() = childColor;
 }

I might be totally wrong on this but that's my best guess. Good luck @Jacobsc

Comment
Add comment · Show 4 · 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 Jacobsc · Nov 30, 2019 at 02:31 AM 0
Share

Thanks! I've been trying to use public but I guess doing ChangeColor would be better. But I get errors with the current child calling function I have.

avatar image WheresMommy Jacobsc · Nov 30, 2019 at 08:04 AM 0
Share

Be sure to check against your parent object, as findcomponentsinchildren also returns the object the script is running on

avatar image Larry-Dietz · Nov 30, 2019 at 05:36 PM -1
Share

FindComponentsInChildren returns an array of components. If you need to change the color in all of the returned components, try something like this...

 foreach ($$anonymous$$aterial m in GetComponentsInChildren<$$anonymous$$aterial>())
       m.Color = childColor;
avatar image Jacobsc · Nov 30, 2019 at 05:41 PM 0
Share

So I changed the script but there are some issues, I'm going to load the new script into another post.

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

133 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

Related Questions

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

Material doesn't have a color property '_Color' 4 Answers

Changing two different objects renderer colour 1 Answer

Detect collision with child object? 2 Answers

Change material of all the children 2 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