how to hide and unhide object
Hi,all. I have a object named obj1 in the scene, now I wish press a button to hide it, then press again to unhide it, I have writed some code bellow, but the code can hide obj1 only, cannot unhide it.Any help plz? thank you very much!
#pragma strict
function OnGUI()
{
if(GUI.Button(Rect(0,0,200,100),"Click Me"))
{
var obj1=GameObject.Find("obj1");
if (!obj1){
obj1.SetActiveRecursively(true);}
else
{obj1.SetActiveRecursively(!obj1.active);
}
}
}
function Start () {
}
function Update () {
}
Can you hide a skybox but still let it light the scene?
Answer by DannyB · Aug 09, 2012 at 04:18 PM
See if renderer.enabled on the game object suits your needs.
EDIT:
renderer.enabled is definitely capable of showing and hiding. Your problem lies elsewhere - your IF statement should check for if( obj1.renderer.enabled ).
Also, you can easily toggle on or off, try the below code (not tested, but you should get the idea).
var cubeRenderer:Renderer;
var obj1:GameObject;
function Start() {
obj1=GameObject.Find("obj1");
}
function OnGUI() {
if( GUI.Button(Rect(0,0,200,100),"my button")) {
obj1.renderer.enabled = !obj1.renderer.enabled;
}
}
Answer by wensk · Aug 10, 2012 at 11:54 AM
Thank you,Danny.I watched this page and used renderer.enabled script,but it still only hide object,not unhide.
#pragma strict
var cubeRenderer:Renderer;
function OnGUI()
{
if(GUI.Button(Rect(0,0,200,100),"my button"))
{
var obj1=GameObject.Find("obj1");
if(!obj1){
obj1.renderer.enabled = true;}
else{
obj1.renderer.enabled = false;
}
}
}
You should use the comments for such posts, not a new answer. Also when posting a question or answer, you should format your code, as it will be easier for people to help you.
I updated the answer above.
Hi, Danny,I have 2 tyres attached to the car, after enter the game, tyreA is hide,tyreB is display.I wish press a button to display tyreA and hide tyreB, press again to hide tyreA and display tyreB, how to do that ? Thanks.
var tyreA : GameObject; var tyreB : GameObject;
if(GUI.Button(Rect(220,340,200,80),"tyre"))
if(tyreA!=null) { { tyreA.renderer.enabled = true; tyreB.renderer.enabled = false; } else { tyreA.renderer.enabled = false; tyreB.renderer.enabled = true; } }
function Start () { tyreA=GameObject.Find("tyreA"); }
function Update () {
}
Answer by flare38888 · Feb 22, 2021 at 01:49 PM
//Use this to hide/unhide.
gameObject.IsActive(false/true);
You can do this or you can do a more complicated thing where to hide it first you find the transform, then delete it and then to get it back create a new one with the same transform.
Your answer
Follow this Question
Related Questions
Receiving a missing reference exception error but everything is still there. 0 Answers
global variables in shader graph?? 2 Answers
Main Menu -> Slider (for music volume) and Back button 0 Answers
Object didnt hide while animation playing 0 Answers
How can I make the character jump between the boxes? 0 Answers