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?

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 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 called SetActiveRecursively(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.