How can I hide a GameObject without active=false?

As far as I know, there are two ways to hide a GameObject.

The most popular way is GameObject.SetActive(false); But this method will disable all functions of it, and that is not my option.

Second way that I didn’t successfully make it worked is GameObject.renderer.enable = false; Because I always got error message said : “MissingComponentException: There is no ‘Renderer’ attached to the “island01” game object, but a script is trying to access it.” when I assign false to it. I don’t understand why. There is a renderer object in GameObject indeed. Why I always got this error message?

I just want hide GameObject without set active property false. I believe that renderer.enable should be the answer, but I just can’t assign value to it. Anybody knows the way?

I found fiddling with the renderer to be not foolproof across 2D/3D.

My solution which looks to be fairly solid is to set the scale to [0] (and in the normal case to work with a scale that is [1]).

// Hide button
GameObject.Find ("ShareButton").transform.localScale = new Vector3(0, 0, 0);

// Show button
GameObject.Find ("ShareButton").transform.localScale = new Vector3(1, 1, 1);

GetComponent<Renderer>().enabled = false is correct. If Unity tells you that there’s no renderer attached to that object, then you can be 100% sure that there is no renderer attached to that object. Most likely the renderer is on a child.

As @Eric5h5 said, GetComponent<Renderer>().enabled = false is correct, but this will only work for 3D objects. If you are trying to do this with UI, then you will want to use GetComponent<CanvasRenderer>().cull = false. (Or if you don’t know which your object is, try both and check the result of GetComponent.)

(Setting the scale to 0 will technically get what you want, but only if you don’t care about performance because the renderer is still going to try and render it. Similarly, moving the object outside of the camera’s view still means the culling logic needs to do math to realize it should be culled.)

Temporarily moving the object to behind the camera might be a solution.