- Home /
how to make my gameobjects disable and enable?
Hi all, i have two game objects in my project. i want to show only one in the scene. i wrote code like this.
using UnityEngine;
using System.Collections;
public class Moon : MonoBehaviour {
public bool onoff;
public Texture2D icon;
public GameObject table;
public GameObject moon;
// Use this for initialization
void Start () {
onoff=false;
}
// Update is called once per frame
void Update () {
if (onoff == true)
moon.active = true;
table.active = false;
if (onoff == false)
table.active = true;
moon.active = false;
}
void OnGUI () {
GUI.Button (new Rect (10,10,100,50), icon);
{
onoff=true;
}
}
}
my intention is i want to show any one by default. if the user click that icon i want to show second one and first becomes turn off. if he again clicks i want first one. like that i want to show.
but i add and run this code by default i didn't get the any one. i add all the things in the inspector view.
please help me.
thank you
navadeep
Answer by BenouKat · Apr 22, 2013 at 09:19 AM
You use "if" without the braces, but there's 2 instructions.
A if without braces but must be followed by only one instruction. In :
if (onoff == true)
moon.active = true;
table.active = false;
if (onoff == false)
table.active = true;
moon.active = false;
Your "table.active = false" and "moon.active = false" are out the if and will be called every frame. Try with braces.
Other things : You button is not correct. Try with this :
if(GUI.Button (new Rect (10,10,100,50), icon))
{
onoff=true;
}
The GUI.Button must be into an if statement. Because GUI.Button return a boolean which indicate if yes or no you push the button. So i guess for your problem :
table active is always false cause out the if. onoff = true in OnGUI is automaticly called because no if statement. so on the update, moon.active = true is called, but moon.active = false is out the if, so neither table nor moon will be active.
Tada.
PS : Prog tips, you don't need to write "if(onoff == true)", just "if(onoff)". Idem for negative comparaison "if(onoff == false)" becomes "if(!onoff)". :)
Benou$$anonymous$$at,
ur Awesome.
I changed my code is like this
using UnityEngine;
using System.Collections;
public class $$anonymous$$oon : $$anonymous$$onoBehaviour {
public bool onoff;
public Texture2D icon;
public GameObject table;
public GameObject moon;
// Use this for initialization
void Start () {
onoff=false;
}
// Update is called once per frame
void Update () {
if (onoff == true)
{
moon.active = true;
table.active = false;
}
else if (onoff == false)
{
table.active = true;
moon.active = false;
}
}
void OnGUI () {
if(GUI.Button (new Rect (10,10,100,50), icon))
{
onoff=true;
}
}
}
but there is problem in that if i clicked the button that is ok. if i again clicked i didn't get table again. First time only it is working.
This is normal. Everytime you click on the button, you set onoff to true. So you can click 309829 times, it will be always set to true :)
Change "onoff = true" by "onoff = !onoff" on your button statement and you done.
Last tip : This problem is very very very basic progra$$anonymous$$g, don't hesitate to read some books or tutorials for learning some basics reflections in progra$$anonymous$$g :)
if(GUI.Button (new Rect (Screen.width-(Screen.width/7),10,100,50), icon))
{
if(!onoff)
onoff=true;
else
onoff=false;
}
i wrote like this it is working fine thanks for help
No problem ;) Don't forget to mark the issue as resolve !
Answer by Sanky · Apr 22, 2013 at 09:24 AM
Dude there is have methods for hide object on any event from the scene gameObject.SetActive (boolean); //this will active or deactivate your object from the scene
gameObject.renderer.enabled = boolean; //this will enable/desable your object mesh