In my game, I have an empty GameObject that has a sprite and a TextMesh as child. I want to access the child (sprite and the text mesh) and turn off the sprite renderer and mesh renderer of the sprite and the text mesh programmatically and be able to then turn it back on, but I don't know how. Some help and/or sample code would be much appreciated. Thanks.

  • \$\begingroup\$ gameObject.renderer.enabled = false/true \$\endgroup\$ – Raxvan Jan 8 '15 at 13:56

You can use 'GetComponent' to access these things. Then, you can edit enable or disable the sprite and mesh renderers.

gameObject.GetComponent<TextMesh>().renderer.enabled = false;

This example will disable the renderer on the TextMesh. Assuming you have a SpriteRenderer on your GameObject (which you refer to as 'Sprite'), to disable that you would use the following code:

gameObject.GetComponent<SpriteRenderer>().enabled = false;

If it is not a SpriteRenderer you can still use the GetComponent method to get the component of your choice and manipulate this component to your likings.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.