Simple but not easy: Hide a Plane and a Cube

Hi guys, I have created a cube and a plane in the Hierarchy panel, I want to hide both from C# script that acts like a “main game manager” attached to an object, I’ve read lots of answers saying use “renderer.enabled”, use “SetActive”, etc., but none is working for me as I always get “Object instance not set to an instance of an object”.

I don’t want to use a “public GameObject” and assign it in the Editor, I want to know how to reference the objects by their name dynamically.

This works fine:

GameObject.Find("mainLight").GetComponent<Light>().enabled = false;

But in the same method, this doesn’t work:

GameObject.Find("theCube").GetComponent<Cube>().enabled = false;

I get: “Object instance not set to an instance of an object”.

Trying:

GameObject.Find("thePlane").GetComponent<Plane>().enabled = false;

“.enabled” doesn’t even exist.

GameObject.Find("theCube").GetComponent<Cubes>().renderer.enabled = false;

Again: “Object instance not set to an instance of an object”.

As I’m writing this question, I wonder if I have to instantiate the cube, sounds logical considering the error thrown, but I have other objects in the Hierarchy panel that I can transform easily, but not the cube or the panel.

Thx in advance.

if you want to disable the renderer on the cube, just do this:

GameObject.Find("theCube").enabled = false;

or

GameObject.Find("theCube").GetComponent<Renderer> ().enabled = false;

I’m pretty sure with SetActive, you actually have to do it like this:

GameObject.Find("theCube").SetActive (false);

You’re trying to grab some component called “Cubes” and “Plane”. If you want to disable the renderer, you need to grab it from the script.

Renderer vs. SetActive:

If you have renderer disabled, then your object will disappear. It will still respond to physics, scripts, etc, but it won’t be shown. If you make SetActive false, it’s literally “destroying” the object from the scene, but you can just enable and disable it (It won’t respond to physics, scripts, etc.).

There’s many different ways to hide an object.