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 /
avatar image
0
Question by tbigfish88 · Nov 10, 2015 at 01:00 AM · materialprefabschild object

How do I set the texture on a child object of an instantiated prefab object?

How do I set the texture on a child object of an instantiated prefab object? I can't seem to find a way to access the child object's components, and the only suggestion I've found on here is to access the child object as a Transform, which I then don't appear to be able to GetComponents from.

If I get the object in question, then calling "...transform.GetChild(0);" gets the child object, but returns a Transform rather than a GameObject. I don't understand why... a Transform apparently only deals with the position, rotation and location of the GameObject. I need to set the texture on this "Transform"... Any help welcome!

Nick

Comment
Add comment · Show 3
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 rutter · Nov 10, 2015 at 01:03 AM 0
Share

All parent-child relationships are specified by the transform hierarchy. You can access children through transform.children, or helper functions like transform.Find(), or you can grab the components directly with functions like GetComponentsInChildren().

the only suggestion I've found on here is to access the child object as a Transform, which I then don't appear to be able to GetComponents from.

Why not? The child is just another GameObject, so you should be able to look up its components if it has any. Are you seeing some error message when you try?

avatar image tbigfish88 rutter · Nov 10, 2015 at 09:02 AM 0
Share

O$$anonymous$$, I'll try that.

When I looked up Transform in the help, it says "It's used to store and manipulate the position, rotation and scale of the object." Why does this object contain links to the children? Seems counter-intuitive at best.

avatar image tbigfish88 rutter · Nov 10, 2015 at 12:17 PM 0
Share

I can't access transform.children. It doesn't exist, apparently. If I get the object in question, then calling "card.transform.GetChild(0);" gets the child object, but returns a Transform rather than a GameObject. I don't understand why... a Transform apparently only deals with the position, rotation and location of the GameObject. I need to set the texture on this "Transform"...

2 Replies

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

Answer by jonofarc · Nov 10, 2015 at 08:29 PM

@tbigfish88

In this case you just need to specify that you want the GameObject from the Transform it would be like

card.transform.GetChild(0).gameObject;

This will get you the child gameObject

then you just have to set the texture

card.transform.GetChild(0).gameObject.GetComponent().mainTexture = "Texture Goes Here";

or

GameObject MyGameObject =card.transform.GetChild(0).gameObject;

MyGameObject.GetComponent().mainTexture= "Texture Goes Here";

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 tbigfish88 · Nov 10, 2015 at 08:36 PM 0
Share

I assume I should be using GetComponent() with the type of $$anonymous$$eshRenderer ? (This works! Thanks! )

avatar image
0

Answer by Statement · Nov 10, 2015 at 08:37 PM

On your instantiated clone, get the transform of the child, then get the renderer of that child transform.

 // Create a prefab, and get the nail child of the clone
   
 var clone = Instantiate(prefab);
 var nail = clone.transform.Find("Left Arm/Hand/Pinky/Nail");
   
 // Ok, not sure why I picked a stupid example, 
 // but get the renderer on that nail and change its
 // renderers materials texture to fancyPolish.
 
 var nailRenderer = nail.GetComponent<Renderer>();
 nailRenderer.material.mainTexture = fancyPolish;
 
 // (It's stupid, because it's stupid to have a renderer 
 // on each nail, btw, imo. But that doesn't affect the 
 // example otherwise)

If it fails, either you have a typo in your child name, path, or it doesn't have a 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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

How to make 2D physics objects graphics? 2 Answers

Shader problem? 1 Answer

Is there a shader I could use to always render a material over another material? 0 Answers

Drag n Drop texture creates material event. 0 Answers

Skybox not correctly alpha blending with custom Shader 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