I have this house where all the different parts are individual objects, and when I click on anything - say a wall - it will get highlighted. I now have this function Code (CSharp): GameObject[] gameobjects = GameObject.FindGameObjectsWithTag(tagName1); foreach (GameObject go in gameobjects) { if (go.GetComponent<MeshRenderer>().enabled == true) { go.GetComponent<MeshRenderer>().enabled = false; go.GetComponent<MeshCollider>().enabled = false; } else { go.GetComponent<MeshRenderer>().enabled = true; go.GetComponent<MeshCollider>().enabled = true; } } where I can hide/show for example all the walls or all the windows/doors etc. but if one of the objects has been highlighted (has the material "selected") I don't want to hide that specific object. The highlighted one should always be visible. does anybody have any idea for how to do this?
You could just reorganise the "if" to ignore something that is highlighted (as long as you know how to check that it is highlighted of course). So, maybe something like this? :- Code (CSharp): foreach (GameObject go in gameobjects) { if( ! add_the_is_highlighted_test_here ) { go.GetComponent<MeshRenderer>().enabled = go.GetComponent<MeshCollider>().enabled = !go.GetComponent<MeshRenderer>().enabled; } } Incidentally, you should really try to limit runtime calls to FindGameObjectsWithTag. You might want to consider having some sort of a manager class that tracks these objects (i.e. the adding and removing of them) and get the list of current objects from that manager instead.
I tried to do something like this Code (CSharp): if (go.GetComponent<Renderer>().material != selected) { go.GetComponent<MeshRenderer>().enabled = go.GetComponent<MeshCollider>().enabled = !go.GetComponent<MeshRenderer>().enabled; } but the selected object still disapears.. what am I doing wrong?
Does the "if" on line 2 definitely work correctly? You may want to step it through in the debugger or add debug logging to find out what is happening.