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.
1 Answer
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.
gameObject.renderer.enabled = false/true
\$\endgroup\$