Mesh Renderer not showing in scene, disabled in Inspector, but debug says it's enabled?

I have the following code. TileNode is a script attached to each hex, and hexes make up the map (turn based strategy).

TileNode[] allNodes = FindObjectsOfType<TileNode>();
foreach(TileNode node in allNodes)
    node.GetComponent<MeshRenderer>().enabled = true;
    Debug.Log("" + node.gameObject.ToString() + "'s mesh renderer enabled: " + node.GetComponent<MeshRenderer>().enabled);

Yet even with this code, as the title says, the inspector shows the mesh renderers as DISABLED, and do not show in the scene (unless I check the box to enable them in the inspector). But the debug.log in my code says “TRUE”. So what the hell is going on, and how do I successfully show the mesh renderers in the scene?


The problem is that FindObjectsOfType will ONLY work when the object it is finding is Enabled. It wont work if the object is INACTIVE or a Prefab.

Make a reference from the gameObject through the script attached to it and save it somewhere as a copy(BACKUP). That backup you will be using later on to do your job. While making a copy of it, Disable them as you mentioned that you want them to be disabled in the beginning. That backup can then be used to enable and disable them.
Below is the code. Make sure all the objects in the scene are ENABLED in the Editor before running this code or it wont work. If you delete everything from the Update Function, you will realize that the object is automatically disabled in the Start Function.

//Private array variable to hold copy of game object. Dont attach anything to it    //from the Editor

	private GameObject[] ObjectBackUp;

	// Use this for initialization
	void Start () {
		TileNode[] allNodes = FindObjectsOfType<TileNode>();

		//Set ObjectBackUp Array Size Based on the amount of FindObjectsOfType //found
		ObjectBackUp = new GameObject[allNodes.Length];  
		for(int i=0; i<allNodes.Length;i++){

			//Copy each allNode To ObjectBackUp
			ObjectBackUp _= allNodes*.gameObject;*_

* //Lets disable All the mesh renderers if you want it to start with //being disabled*
_ allNodes*.GetComponent().enabled = false;
Debug.Log(allNodes.gameObject.ToString() + "'s mesh renderer enabled: " + allNodes.GetComponent().enabled);

* //Garbage Collector takes down allNodes, ObjectBackUp LIVES for us so that //we can access ot later on*

* //Not Neccessary but lets show and hide object every 2 seconds*
* StartCoroutine(showOrHideObject());*
* }*

* // Update is called once per frame*
* void Update () {*

* //You can Enable/Disable each one of them now after by using ObjectBackUp*
* //ObjectBackUp1.renderer.enabled = true;*
* //ObjectBackUp[4].renderer.enabled = true;*
* }*

* IEnumerator showOrHideObject(){*
* while(true){*
* //Or you can Enable them all with Loop*
* for(int i=0; i<ObjectBackUp.Length;i++){*
* //true or false will turn them on/off*
_ ObjectBackUp*.renderer.enabled = true;
yield return new WaitForSeconds(2.0f);
//Or you can Disable them all with Loop*

* for(int i=0; i<ObjectBackUp.Length;i++){
//true or false will turn them on/off*

ObjectBackUp*.renderer.enabled = false;
yield return new WaitForSeconds(2.0f);

You will notice that I stripped out foreach statement. That’s right. Don’t use it with Unity unless you are using Microsoft Visual Studio to compile. If you are just using the IDE that comes with Unity, then use for statement instead of foreach or else it will bite you later on when you are working on a big project.