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.