Double Input.GetButton problem.
Hi guys, I'm new with Unity so don't be too harsh on me :D
I have two game objects, they are turned off at the start. With Input.GetButton("X button") i want to turn ON game object number one. When I use Input.GetButton("X button") another time I want game object number one to turn OFF, and the game object number two to turn ON. If I click again, number two gets OFF , and number one gets ON. And so on. I'm using SetActive function.
If anyone can lead me to how to go about doing this it would be greatly appreciated!
Answer by Oribow · Oct 02, 2015 at 09:25 PM
public GameObject g1;
public GameObject g2;
bool isG1On = false;
void Update()
{
if (Input.GetButtonDown("Fire1"))
{
if (isG1On)
{
g1.SetActive(false);
g2.SetActive(true);
isG1On = false;
}
else
{
g1.SetActive(true);
g2.SetActive(false);
isG1On = true;
}
}
}
I kinda need my object two, to be ON when i push my button once, not when i hold my button pushed. if you know what I mean ;)
Thats excatly what the code does. $$anonymous$$ay you explain your self a bit more? The state of both objects are switch when you press your button once, no need for holding.
Ups, my bad, wrong self explanation of "GetButtonDown" function. This is working perfectly. Just one more thing. Can we use something else beside "else" statment, becouse I have read that using "else" statment is bad practice in Unity. Fix me if I am wrong.
You are indeed wrong. "Else" is a basic keyword found in every program$$anonymous$$g language. Its say just, that if the first "if()" statement is wrong, then do the stuff in the "else{}" brackets. Do you have the source from where you read that? Would be interesting :) Besides that, I can promise you, this is the fastest solution possible. :)
Your answer
Follow this Question
Related Questions
gameObject.SetActive (false); Not working 0 Answers
gameObject.SetActive (false); Not working 0 Answers
Reset rotation of gameobject 1 Answer
Make a object always appear behing another object 1 Answer
How do you activate a game object ? 1 Answer