- Home /
How can I control GameObject visibility?
I've searched around on the forums and online, and this would seem to be a simple question from what I found. The best method I found for controlling visibility of GameObjects was setting
myObject.renderer.enabled = false; //makes object invisible myObject.renderer.enabled = true; //makes object visible
This method actually works most of the time for me. However, in my game, I use a Reset method (that I created), which is called by a BroadcastMessage sent when the player dies. This Reset method is supposed to reset all objects in the level. It is getting called, but the contents of it are not working. It is important to note that the GameObject I am using has an attached animation as well, which I attempt to Rewind in this method as well to no avail. Here is the specific code for the Collectable class I have:
protected bool Reset()
{
animObject.renderer.enabled = true;
if (animObject && animObject.animation.GetClip("collect"))
animObject.animation.Rewind("collect");
if (this.animation && this.animation.GetClip("collect"))
this.animation.Rewind("collect");
return true;
}
Using Debug statements I ascertained that the method IS getting called, and the renderer.enabled variable is getting set to true. However, when I pause the game, the mesh renderer is disabled in the editor! And regardless of whether I pause or not, the GameObject I was trying to set visible again never shows up, but IS in the right spot and is collidable.
It perplexed me further when I found that setting the renderer.enabled variable worked just fine in other methods, but whenever I called it from this Reset method it didn't work. None of the other GameObjects being reset with the BroadcastMessage affect these collectables that should be getting reset.
Am I doing something wildly wrong?
Answer by rutter · Apr 03, 2012 at 06:09 PM
I could be missing it, but I don't think the problem is in the particular bit of code you've posted. With what little I know, here, the first few things I'd check are:
Are you calling
BroadcastMessage()
on every object you're trying to reset? The function only operates on one object and its children.If you are iterating objects to call
BroadcastMessage()
in a loop, it's important to remember that many of Unity's "find" functions won't necessarily return disabled objects. (Have you calledSetActiveRecursively(false)
on anything?)Is it possible that some other function(s) are messing with your renderers after the reset call?
This may be a good chance to practice using MonoDevelop's built-in debugger. You can attach it to your Unity process, add a breakpoint around the time of the reset call, and watch your code go line-by-line. It's time consuming, but it'll help you understand exactly what your code is doing.
It turns out someone else's code was messing with the mesh after I Reset the object. It was being flagged for destruction again even after Reset, so was going invisible again immediately. Sorry for making this seem like a complex question, it just took me a while to find the error.
Answer by garner · Mar 31, 2012 at 01:18 PM
You never set
animObject.renderer.enabled = false;
so it'll never hide. If it's disabled in the editor it's because you need have the gameobject unchecked when the play button is off because there's no code here to disable it.
I'm sorry, I thought I made it clear in the question that I used exactly that statement and it worked just fine. Just to be clear, I do set it to false, and it does work. Getting it to re-enable is my problem, and only during this Reset method.
Your answer
Follow this Question
Related Questions
Hide gameobject until certain condition has been met,Hide an object until a condition is met. 2 Answers
Rendermode: Cutout not working on gameobject from external asset bundle 2 Answers
Help! Skinned mesh render not visible!! 3 Answers
Unity3D C# enable renderer for a few seconds 1 Answer
Material class use general question 1 Answer