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 MRxBIGxSTUFF · Oct 21, 2016 at 05:40 PM · unity 5renderer

Find Renderer Attached To Parent Object Runtime

Hi guys,

I have hit a wall trying to find the renderer to my other player.

Im developing a multiplayer game and need to access the other players mesh renderer then swap the shader. I have tried a number of things but non seem to work i keep getting renderer is null unless i manually drag the renderer in at runtime..

The renderer sits on the players mesh in the parent.

Here is what i have so far.

 using UnityEngine;
 using System.Collections;
 
 public class KeepShader : MonoBehaviour {
 
     public Shader shader;
     public Renderer rend;
     public GameObject remotePlayer;
 
     void Update() {
 
         remotePlayer = GameObject.FindGameObjectWithTag("Player");
         remotePlayer.GetComponentInParent<Renderer>();
         rend.material.shader = shader;
 
     }
 }

I know the rend reference to the renderer is obviously null in this example but i just can not work out how to access it in the parent at runtime and assign the rend, Any help appreciated guys.

I have read multiple examples and the unity documents but nothing seems to work.

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 andzq · Oct 21, 2016 at 06:22 PM 0
Share
 rend = transform.parent.GetComponent<Renderer>();

this returns the renderer of the parent of the gameobject holding your $$anonymous$$eepShader-script. dunno if this is exactly what you want. if you try to access the remotePlayer's parent's renderer it would be

     rend = remotePlayer.transform.parent.GetComponent<Renderer>();
avatar image andzq · Oct 21, 2016 at 06:28 PM 0
Share

also i would suggest you do not swap the shaders in your update function, since this will probably result in a flashy stroboscope effect. rather check in update for a condition that triggers a function containing your shader swap.

 void Update(){
 if (condition)
 SwapShaders();
 }
 
 void SwapShaders(){
  //swapCode
 }
avatar image MRxBIGxSTUFF andzq · Oct 21, 2016 at 06:39 PM 0
Share

Thanks for the answer that seems to work, Next problem is that finds the "body" gameobject the mesh sits as a child to that how do you access that?

avatar image andzq MRxBIGxSTUFF · Oct 21, 2016 at 09:47 PM 0
Share

you can access the child of a gameobject by using:

 GameObject child = body.transform.GetChild(0).gameObject;

this returns the first child of the body-gameobject.

you can also iterate through all childs of this body object by using

 foreach(Transform child in body.transform)
 (if child.name == "name from hierarchy")
 // -> whatever you wanna do to the child


1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by MRxBIGxSTUFF · Oct 21, 2016 at 09:46 PM

Thanks andzq for the initial answer which helped me solve the question which i just needed to adjust to my needs. i did the following to find the mesh within the parent.. rend = remotePlayer.transform.parent.GetComponentInChildren();

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

115 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

Related Questions

how to make a cube change color by clicking a button? 0 Answers

Make shader from Blender nodes 0 Answers

LineRenderer with Animation curve distorting texture? 0 Answers

Gameobject Renderer not working. 1 Answer

Renderers looks pixelated (mesh and line ) 0 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