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 Zeon04 · Oct 18, 2014 at 04:52 PM · materialcomponentrendererparent-child

How do I access the renderer of a child from the parent gameObject?

Hi.

I'm having a problem with my script trying to access the renderer but the renderer is in the child (CModel - Transform, Mesh Filter, Mesh Renderer components) and not the parent (Character - Transform, Collider, Rigidbody, movement script, material switch script) so I'm getting this error:

MissingComponentException: There is no 'Renderer' attached to the "Character" game object, but a script is trying to access it. You probably need to add a Renderer to the game object "Character". Or your script needs to check if the component is attached before using it.

Below is the script attached to the parent, trying to access the renderer (it switches between 2 materials on a mouse click):

     public Material[] materials;
     int index = 0;
 
     // Use this for initialization
     void Awake () 
     {
         renderer.material = materials[index];
     }
 
 
     // Update is called once per frame
     void Update () 
     {
         CheckMouseInput ();
         playerShift ();
     }
 
 
     void CheckMouseInput ()
     {
         if (Input.GetMouseButtonDown (0)) //if left mouse button (0) is clicked
         {
             //code to check that player touch a specified area using ray casting. Line drawn from camera, if it hits our collider run the code.
             
             Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition); //creates a screen position when mouse button clicked down, ray goes there
             RaycastHit hitInfo; //holds info from ray cast
             
             if(Physics.Raycast (ray, out hitInfo))//if Physics ray hits, push out hit info
             {
                 
                 index++;
                 if(index >= materials.Length)
                     index = 0;
                 renderer.material = materials[index];
             }
         }
     }
     
     void CheckTouchInput()
     {
         if (Input.touchCount > 0) 
         {
             if (Input.touches [0].phase == TouchPhase.Began) 
             {
                 Ray ray = Camera.main.ScreenPointToRay (Input.touches [0].position); //creates a screen position when mouse button clicked down, ray goes there
                 RaycastHit hitInfo; //holds info from ray cast
                 
                 if (Physics.Raycast (ray, out hitInfo)) //if Physics ray hits, push out hit info
                 {
                     index++;
                     if(index >= materials.Length)
                         index = 0;
                     renderer.material = materials[index];
                 }
             }
         }
     }
 
     void OnMouseDown()
     {
         index++;
         if(index >= materials.Length)
             index = 0;
         renderer.material = materials[index];
     }
 

I've tried using GetComponentsInChildren but not sure where to put it so I still get the same error.

Tried adding a renderer component to the parent but that stops the material switching.

What am I missing?

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 b1gry4n · Oct 18, 2014 at 04:55 PM 0
Share

If your child object never changes why not just make a public reference to the object and drag/drop in the inspector. Now you have a direct reference

when you type "renderer.material" you are calling the object that this script is on. The error you are getting is because the script is looking for a renderer on your character game object. If you dont want to use a direct reference it needs to be something like :

 private $$anonymous$$eshRenderer childRenderer;
 
 void Start(){
 childRenderer = GetComponentInChildren<$$anonymous$$eshRenderer>(); // this is assu$$anonymous$$g you only have 1 renderer in the children
 }
 
 // when you want to change its material.
 
 childRenderer.material = //whatever material you want. $$anonymous$$eep in $$anonymous$$d this will make an instance of the material. If you want to use a global shared material use:
 
 childRenderer.shared$$anonymous$$aterial = //whatever


Also, if all you are switching between are 2 materials why not make it a little simpler? Add some bools "materialA" "materialB"

 public $$anonymous$$aterial matA;
 public $$anonymous$$aterial matB;
 private bool materialA = false;
 private bool materialB = false;
 public $$anonymous$$eshRenderer childRenderer;
 
 void Start(){
 materialA = true;
 childRenderer.material = matA;
 }
 
 
 if(materialA){ // if the current material is matA, switch to matB
 materialA = false;
 materialB = true;
 childRenderer.material = matB;
 }else if(materialB){// if the current material is matB, switch to matA
 materialA = true;
 materialB = false;
 childRenderer.material = matA;
 }

avatar image Zeon04 · Oct 20, 2014 at 03:04 PM 0
Share

Hi. Thanks for the reply. I'm not very good at coding so could you explain it a bit more please?

With a direct reference, do I put the parent or the child object in? Will that fix the problem or do I need to put in GetComponentInChildren code?

Is my render.material = materials[index] wrong?

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by 767_2 · Oct 18, 2014 at 05:10 PM

make a renderer array

   public Component[] Renderer;

use this line where you want to get renderers in your children

 Component = gameObject.GetComponentsInChildren<Renderer>();

and they are now stored in the array

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 Zeon04 · Oct 20, 2014 at 03:11 PM 0
Share

Hi. Thanks for the reply. I'm not very good at coding so could you explain it a bit more please?

Which parts of my code do I replace with yours?

Will your code work with my existing material array?

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Changing two different objects renderer colour 1 Answer

SetPropertyBlock doesn't work for particle systems? 0 Answers

Why does my materials get destroyed on some persistant objects ? 2 Answers

Problem with sprite animation on android 1 Answer

Combine materials out of renderer.materials 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