- Home /
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?
Thanks
Answer by hangemhigh · Sep 08, 2014 at 10:30 AM
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.
Solution? 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[i] = allNodes[i].gameObject;
//Lets disable All the mesh renderers if you want it to start with //being disabled
allNodes[i].GetComponent<MeshRenderer>().enabled = false;
Debug.Log(allNodes[i].gameObject.ToString() + "'s mesh renderer enabled: " + allNodes[i].GetComponent<MeshRenderer>().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
//ObjectBackUp[1].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[i].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[i].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.
Your answer
![](https://koobas.hobune.stream/wayback/20220613161205im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
renderer.enabled question 2 Answers
Turn off renderer untill key pressed again 0 Answers
Changing two different objects renderer colour 1 Answer
How do I make gameobject invisible (2D)? 1 Answer
How to make an ocean by modifying a mesh 3 Answers