Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by paco morales · Jan 19, 2013 at 02:28 AM · guifunctionswitch3dobject

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;
 
     }
 }
 
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
2

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;
     }
 
 }
Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image justin35f · Jan 19, 2013 at 03:42 AM 0
Share

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.

avatar image paco morales · Jan 19, 2013 at 03:49 AM 0
Share

O$$anonymous$$ Guys, let me try it. Thanks

avatar image
1

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.

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image paco morales · Jan 19, 2013 at 04:56 AM 0
Share

Thanks guys now is working :)

avatar image paco morales · Jan 19, 2013 at 05:50 AM 0
Share

Thanks again @eXtremeTY, and Justin Hammond :)

http://www.youtube.com/watch?v=$$anonymous$$$$anonymous$$E5OxgSPJ4

avatar image justin35f · Jan 19, 2013 at 05:58 AM 0
Share

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?

avatar image paco morales · Jan 19, 2013 at 06:44 AM 0
Share

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://youtu.be/GU0AF3l3h2w

http://paco-morales.blogspot.mx/

Thanks Again,

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

11 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

A node in a childnode? 1 Answer

Change Model On GUI Button Click. 1 Answer

EditorGUI and method create 0 Answers

How to return from the last case to the first 1 Answer

function GUI.DrawTexture with OnMouseEnter/Exit to change texture all within script? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges