Making Objects Invisible by Using renderer.enable does not work

Hey guys. I’ve played around with this for hours. But it still doesn’t work.

I try to move some spheres inside a cube. When the spheres collide with cube, it disappears.

function OnCollisionEnter(collision : Collision)
{
	if(collision.gameObject.name == "Air Bubble")
	{
		collision.gameObject.renderer.enabled = false;
	}
}

I think the problem comes from the collision detection. Because when I try to iterate all of them and make their renderer’s false. It works. But when I use gameObject.renderer.enabled = false. They will not work.
Basically, setting individual object’s renderer to false will not work.

Any suggestion on how to solve the detection problem?

Don’t rely on the auto-generated name. For example if your cloned object hold a reference to his own prefab, the reference will be changed when he’s instantiated. If the clone create another instance it will clone itself instead of the original prefab. The name will be “Air Bubble(clone)(clone)” in that case.

You can either set a fix name when you instantiate the objects, or use a tag name instead of the object name.

example:

var clone : GameObject;
clone = Instantiate(prefab);
clone.name = "Air Bubble";

Another problem could be that if your object have sub objects also with colliders, you can collide with them and you will get the name of this subobject instead.

To check what the problem is you can temporarily put a Debug.Log("Collided with :" + collision.gameObject.name); in your OnCollisionEnter function. It will print the name of the object you just collided with.

one problem i believe your having here is that your using air bubble(clone) this has caused me problems myself but you can fix this by using tags you could use
airbubble = GameObject.FindGameObjectWithTag(“yourtag”);
and then after doing that in your OnCollisionEnter(collision : Collision)
change that to OnTriggerEnter
so then it should look like this:


var airbubble : GameObject;

function OnTriggerEnter (hit : Collider)
{
    airbubble = GameObject.FindGameObjectWithTag("airbubble");   


	if(hit.gameObject == airbubble)
	{
		hit.gameObject.renderer.enabled = false;
	}
}

i’ve tested this i know that this works

take out the gameobjects.

function OnCollisionEnter(collision : Collision)
{
    if(collision.renderer.name == "Air Bubble")
    {
       collision.renderer.enabled = false;
    }
}

I use c#, but this looks right to me.

Hope this helps somebody:

 track.renderer.enabled=false; //didn't work

 //I had to use:    
 track.renderer.active=false;  //this worked

I’ve been trying unity for a couple months now, but I’m getting tired of it. I’m hammering on just because they deal with the low level 3D-hardware startup.

What I had to do was restart Unity and now it works.