- Home /
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?
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;
}
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?
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
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?