- Home /
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?
Have you checked whether this function is called at all? And if it is, is the if()-condition ever met? Add two Debug.Log messages to confirm that.
$$anonymous$$y guess is a problem with the collision detection. Your spheres might be too small and too fast, so they never collide with the cube's shell during frames. Search the Wiki for a script named "DontGoThroughThings"
Thanks. The function is called. I see the debug info. How do I know they are too small or too fast?? They are moving in a speed that I can view. They all have scale(1,1,1). Are they small?
Do none of the bubbles disappear, or just some of them? Is the "if" condition above ever met (see my comment, put another Debug.Log in there)? What are the dimensions of your cube as opposed to your spheres? Is the cube a Unity primitive, or is it a model?
If the spheres move to fast (essentially, if in one (physics-)frame they are fully inside the cube, but the next (physics-)frame would put them fully outside), the physics engine doesn't receive any collision events. The "DontGoThroughThings" makes sure these situations are catched.
Answer by Bunny83 · May 26, 2011 at 01:03 AM
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.
Thank you for your answer. I tried everything......it still doesn't work. I tried to rename all the clone objects as you said. Only 1 or 2 of the clone objects' renderer set to false.
BTW, i also used the debug info. It does display the correct collision info.
It displays 7 collision info...but only 3 air bubbles disappear. can't figure out why...have worked on it the whole day...
If the renderer gets disabled it should disappear. Are you sure that you don't enable it somewhere else? Remember, you can pause the game with the pause button at the side of the play button. Then you can check the renderer state of your objects and other stuff.
All those "bubbles" are instantiated from the same prefab, right? Or do you have different types/prefabs?
I used the pause button. That's how i know only 1 or 2 of them are set to false. All those bubbles are instantiated from one prefab.
Answer by hastenshawn · May 26, 2011 at 01:14 AM
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
Thanks for answering my question. I've tried to use tags before I asked the question. And it still doesn't work. I don't understand why you choose OnTriggerEnter ins$$anonymous$$d though it doesn't work either.
Sorry, but your example doesn't make much sense. FindGameObjectWithTag will find only the first gameobject with that tag. If you collide with another bubble it won't work. You should compare the tag name of the object you hit.
if (hit.gameObject.tag == "airbubble")
also i don't see any reason why you should use a trigger ins$$anonymous$$d of a normal collider. If it's a rigidbody you can't turn the collider into a trigger.
Answer by ccraig · Apr 10, 2013 at 12:06 PM
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.
Answer by volkswagenb · Jul 17, 2013 at 07:06 PM
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.
Your answer
