- Home /
How to make an object invisible
I've been trying to use the following code to make objects invisible:
renderer.enabled = false;
However it doesn't make any difference and is still visible in the scene. I need it working so that if the players objective is 0, the objective is invisible but if the player's objective is 1 or higher, the object becomes visible again.
![alt text][1] [1]: http://25.media.tumblr.com/tumblr_marg19JK751qjvatjo1_500.png
What I want is to make the chain of the mug disappear until the first objective is complete, but at the moment the code isn't working. Could someone make an example Unity project of like a disappearing cube?
Most of my project is using Javascript. Not sure if this is what's causing all the problem or if it's something else.
Problem solved. Thanks for the help everyone :)
I'm guessing that your script is disabling the wrong renderer - because disabling a renderer will make an item invisible!
Hey $$anonymous$$Fili,
That code works perfectly for me. Could you send me the test project via www.wetransfer.co.uk ?
$$anonymous$$y email is $$anonymous$$ithosAnnar(at)gmail.com.
I will look at your project and tell you what your'e doing wrong there.
Cheers!
$$anonymous$$ithos
P.S. Don't forget to zip it before you upload it!
Can we please try to keep discussions on Unity Answers - this is a knowledge base and is useful for searchers in the future - if you take a discussion to email the result and the process of finding it will be lost.
Hi Whydoidoit,
The idea is that I download it and post the answer on how to solve the issue here on unity answers as detailed as I can.
The email, is just so he can send it directly to me.
Cheers!
$$anonymous$$ithos
$$anonymous$$Fili,
Btw,
If you don't want to send me the project, then 2 renderers on an object can also be the problem.
Something that will always work is:
gameObject.active = false;
You can't get more invisible than that.
Cheers!
$$anonymous$$ithos
Answer by MithosAnnar · Sep 22, 2012 at 05:33 PM
Hi Alex,
I made an example scene for you:
https://dl.dropbox.com/u/107533178/DissapearingCubeExample.zip
There is a script on the camera that accesses the cubes renderer. Really basic stuff.
Cheers!
Mithos
Thank you. Looks like I need to go back to basics and try the simple things first!
Your'e Welcome!
Did it work well for you?
If it did I m glad!
Cheers!
$$anonymous$$ithos
Yep the example you provided did work. Just wondering, is it sensible to mix C code and Javascript code in the same Unity project? Since most of my code is in Javascript. Not sure if that's why I had those problems before.
Don't mix them - due to compilation order you will always be running into problems. If you get scripts from elsewhere that are in C# put them in a Plugins or a standard Assets folder so you can access them, with your own code it is harder because there are likely to be two way dependencies and that is not possible.
@mithos can you paste the code here rather than a Dropbox link that may one day break rendering your answer useless to future browsers? Thx!
Answer by SergeantBiscuits · Sep 22, 2012 at 03:34 PM
I'm going to assume you made the same mistake I did when I first started using Unity; you have a GameObject with a bunch of children inside of it. You need to disable the renderers of all the children!
Here's an example script that will make the object visible or invisible depending on whether or not "isVisible" is true:
var isVisible : boolean = true;
function Update(){
if(isVisible){
var allMyRenderers = GetComponentsInChildren(Renderer);
for (var renderer : Renderer in allMyRenderers) {
renderer.enabled = false;
}
}
else{
var allMyRenderers = GetComponentsInChildren(Renderer);
for (var renderer : Renderer in allMyRenderers) {
renderer.enabled = true;
}
}
}
This will loop through all of the object's renderers and disable 'em. To make them visible again, use the same code but with 'true!'
Still struggling. Now it's saying: $$anonymous$$issingComponentException: There is no 'Renderer' attached to the 'Chained$$anonymous$$ug' game objectm but a script is trying to access it. You probably need to add a Renderer to the game object "Chained$$anonymous$$ug". Or your script needs to check if the component is attached before using it.
This doesn't make sense to me, if it's not got a renderer component then why I can see it on the game? :s
Also getting this error: ArgumentException: You are not allowed to call get_gameObject when declaring a variable. $$anonymous$$ove it to the line after without a variable declaration.
$$anonymous$$ake sure the code I supplied goes somewhere in the Update() function, including the var declaration. I updated my answer.
It's better to avoid a variable called renderer, since this name is used in the GameObject.renderer property. Give another name to the variable, like render. Another point: you can simply assign isVisible to the property ins$$anonymous$$d of using if:
var isVisible : boolean = true;
function Update(){
var all$$anonymous$$yRenderers = GetComponentsInChildren(Renderer);
for (var render : Renderer in all$$anonymous$$yRenderers) {
render.enabled = isVisible;
}
}
Could someone make an example Unity project of like a disappearing cube?
Answer by udaanparvaz · Sep 23, 2012 at 02:41 PM
You can always set the local scale of the gameobject to 0 if memory is not the problem.... gameObject.transform.localScale = Vector3(0,0,0);
Thanks! This works great as a quick fix. Would probably cause some problems with collisions and things like that... but it works really well :D
Your answer
![](https://koobas.hobune.stream/wayback/20220613081334im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
A node in a childnode? 1 Answer
Imported models partially disappear 0 Answers
Blender 2.62 FBX not importing 2 Answers
Renderer on object disabled after level reload 1 Answer
Part of a model is visible while the rest is invisible. Glitch? 2 Answers