You have problems with several problems. First, you are trying to use GetComponent<> for a class instead of an instance of an object. This is directly related to your second problem. After searching for a specific GameObject you are not using the result, and you are trying to disable the GameObject renderer containing the script. Third, C # is case sensitive, Renderer is a class, and Renderer is a reference to a Renderer instance attached to a GameObject
This piece of code combines everything: find a GameObject and turn off its rendering
GameObject go = GameObject.FindWithTag("zone1"); if (go != null) { // the result could be null if no matching GameObject is found go.renderer.enabled = false; }
You can use go.GetComponent<MeshRenderer>().enabled = false; instead of go.renderer. enabled = false; go.renderer. enabled = false; But using Renderer , you donโt need to know what kind of rendering GameObject uses. For example, it can be MeshRenderer or SpriteRenderer , Renderer always points to the rendering used by GameObject , if one exists.
source share