- Home /
Assigning two function to a GUI button?
Hello everyone,
I'm trying to animate or move a switch button in my game scene. So when I hit my "GUI on/off button" it makes the "3d on/off button in orange" change the position to ON.
But I want to hit the same button, the "GUI on/off button" to change to the original position to OFF.
Now I can move the 3d on/off button to the ON position when I hit the GUI on/off button. But I can't move it back to the position OFF.
Please take a look to my video and my scripts.
Any advice is greatly appreciated
http://www.youtube.com/watch?v=7eX0WkyVRqI
Switch Script
var smooth = 1;
var on : Transform;
function Update () {
if(buttonsGUI.onGo ==1){
transform.position = Vector3.Lerp (transform.position, on.position, Time.deltaTime * smooth);
}
}
GUI On/Off Switch Script
static var onGo : int;
var Skin_power : GUISkin; var beep : AudioClip;
function OnGUI (){
GUI.skin = Skin_power;
if (GUI.Button(Rect(870,227,80,56),"")){
audio.PlayOneShot(beep);
onGo = 1;
}
}
Answer by eXtremeTY · Jan 19, 2013 at 02:51 AM
I am not sure if I understood your question correctly, but if I have, could this be a solution?
static var onGo : int;
var Skin_power : GUISkin; var beep : AudioClip;
var switchedOn : boolean = false;
function OnGUI()
{
GUI.skin = Skin_power;
if (GUI.Button(Rect(870,227,80,56),"") && !switchedOn)
`` {
// switch on do your stuff..
audio.PlayOneShot(beep);
onGo = 1;
switchedOn = true;
}
else if (GUI.Button(Rect(870,227,80,56),"") && switchedOn)
`` {
// switch off do your stuff..
switchedOn = false;
}
}
I agree with @eXtremeTY, this should do it for you. Also, with this approach, there is no need for the onGo variable either. Personally I would wrap your code in a function and call that function from the GUI.Button code.
Answer by justin35f · Jan 19, 2013 at 04:20 AM
Ok, I looked at this again, and even though I agree with @eXtremeTY, I would do it like this. Use this for your button code:
var Skin_power : GUISkin;
var beep : AudioClip;
function OnGUI() {
GUI.skin = Skin_power;
if(GUI.Button(Rect(870,227,80,56),"")) {
audio.PlayOneShot(beep);
buttonSlide();
}
}
Then use this for your movement: var onGo = true; var smooth = 1; var on : Transform; var off : Transform;
function buttonSlide() {
if(onGo) {
transform.position = Vector3.Lerp (transform.position, on.position, Time.deltaTime * smooth);
onGo = !onGo;
}
else {
transform.position = Vector3.Lerp (transform.position, off.position, Time.deltaTime * smooth);
onGo = !onGo;
}
}
Now you will need to pass in another transform with this method, for your initial position, so it knows where to move. I would probably move the audio code into the buttonSlide function as well. Also, if you use a GUIButton, it's better to create a function, rather than use the update function, as this just wastes CPU cycles checking stuff in the update, when you know you only want it done when the button is pressed.
Thanks again @eXtremeTY, and Justin Hammond :)
http://www.youtube.com/watch?v=$$anonymous$$$$anonymous$$E5OxgSPJ4
Awesome, glad we could help! Just a note though, maybe try slowing the speed of the animation down, rather than instantly stopping it, to make it look more natural. Either way, looks good. Just curious, what kind of project are you working on?
I make animations and illustrations for products like electronics, tools, medical instruments,devises, etc. Now I want to make "interactive-demos"
That's why I'm learning Unity :)
I want to practice with a device that I can show to my clients as an example. I know I need to improve my 3d model, but until I finished the scripting.
http://youtu.be/ZeAsOeqD$$anonymous$$p4
http://paco-morales.blogspot.mx/
Thanks Again,