- Home /
Toggle button help
hi can anyone make a scipt that does so i can press a button on my keyboard to toggle a objects visibility example: i press Ctrl and a object (no matter where i am) will get removed if i then press Ctrl again it will come back or if i press Shift it will then come back
Answer by Rennat · Dec 02, 2010 at 04:45 PM
use Input.GetButtonDown
since it returns true
only on the frame the button is pressed, not every frame while the button is pressed.
if (Input.GetButtonDown("Fire1")) {
// Toggle code here
}
To make something invisible you can deactivate the gameObject
gameObject.active = false;
in a script gameObject
refers to the game object that the script is attached to, which you don't want to do because it will stop scripts form executing on that game object and then you will only be able to toggle to false
and then the script wont run. If you want to work with another game object you need to add a variable of type GameObject
to the top of the script (outside all functions) like
var myGameObject : GameObject;
Then you'll need to link that property to the target game object in the inspector.
and a one liner for toggling true
/false
states:
myGameObject.active = !myGameObject.active;
ok can you put the script together please im not so good at scripting
I'm not here to make it for you, I'm here to $$anonymous$$ch you to make it yourself. If you're just wanting someone to write your scripts for you then you'll need to hire someone.
Your answer
Follow this Question
Related Questions
Can someone help me fix my Javascript for Flickering Light? 6 Answers
Double tap to run 1 Answer
3 sets of button activations GUI 1 Answer
Door key scripting problem 0 Answers
Opening an animated door with a keycard 0 Answers