- Home /
Question by
Aurelye · Jan 17, 2012 at 11:09 AM ·
raycastchildrendermouseclick
Turn off the rendering of a child with raycasting
What is wrong with this script?
public void CheckObject()
{
RaycastHit hit = new RaycastHit();
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Input.GetKeyDown(KeyCode.Mouse0))
{
Debug.Log("Mouse clicked");
if(Physics.Raycast(ray, out hit))
{
if (hit.collider.CompareTag("ItemToFind"))
{
hit.collider.gameObject.renderer.enabled = !enabled;
Debug.Log("Renderer disabled");
ParticleRenderer myParticles = hit.collider.GetComponentInChildren<ParticleRenderer>();
myParticles.enabled = !enabled;
Debug.Log(myParticles.name + " should now be rendered");
Destroy(hit.collider.gameObject, 2);
Debug.Log("Yay you found an object!");
}
else
Debug.Log("We are not looking for this object");
}
}
}
When I run the script if I click on an object with the required tag it will no longer be rendered, the child component, however, does not get rendered. The debug log tells me the name of the child object, and the thing I clicked on is then destroyed.
I found a work around by instantiating the child object at this point instead of rendering it, however I don't understand why it won't let me tell it to render, I get no error messages and earlier, when I was using on mouseover instead of raycasting, turning on/off the rendering worked fine. I'm quite new to scripting though, so I'm sure there's something I'm missing here.
Can anyone explain it to me?
Comment