- Home /
Question by
ahgr123 · Aug 13, 2015 at 03:14 PM ·
gameobjectscriptingbasicsactive
Deactivating/activating gameobjects on button press
Hi, i am still learning scripting so help would be appreciated.
What i am trying to achieve is:
-when scene starts aaa object is deactivated and bbb activated -THIS WORKS
-when button is pressed i want aaa object activated and bbb deactivated - THIS STILL WORKS
-when button is pressed again i want aaa deactivated again and bbb activated again - player could switch between these infinitely - But that part doesn't work and Unity doesn't give me any errors.
I work with Unityscript.
#pragma strict
var aaa : GameObject;
var bbb : GameObject;
function Start()
{
aaa.SetActive(false);
}
function Update ()
{
if(Input.GetMouseButtonDown(2)) {
bbb.SetActive (false);
aaa.SetActive (true);
}
if(Input.GetMouseButtonDown(2) && bbb.SetActive == false && aaa.SetActive == true) {
bbb.SetActive (true);
aaa.SetActive (false);
}
}
Comment
Forgot to mention that script isnt attached to that gameobject so this isnt the case why it diesnt work.
Best Answer
Answer by elitcube · Aug 13, 2015 at 07:23 PM
#pragma strict
var A : GameObject;
var B : GameObject;
var On : boolean;
function Update () {
if (Input.GetKeyDown(KeyCode.O)) {
if (On == true) {
On = false;
} else {
On = true;
}
}
if (On == true) {
A.SetActive(true);
B.SetActive(false);
} else {
A.SetActive(false);
B.SetActive(true);
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220611235723im_/https://answers.unity.com/themes/thub/images/avi.jpg)