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 Zer0Frost · Jan 14, 2015 at 03:13 PM · uibuttonobjectrotate

How to rotate boject using UI button?

I want to make UI button that after clinking on it and hold will rotate object.

I wrote script but it only rotates object everytime button its clicked. Seem like Update function doesnt work after writting ("float a")

    public void Update (float a) 
         {
             if(a==1)
                 transform.Rotate (new Vector3 (0, 10, 0));
         }

How to make object rotate whilke UI button being held down?

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

3 Replies

· Add your reply
  • Sort: 
avatar image
3
Best Answer

Answer by Cocobongo · Jan 14, 2015 at 03:49 PM

The Update() method needs to remain parameterless, or Unity won't call it.

As @Mmmpies said, you could use a boolean variable to toggle between rotating and not rotating states.

So, all in all, something like:

 private bool _rotate;
 
 void Update()
 {
     if(_rotate)
     {
         transform.Rotate(new Vector3(0, 10, 0));
     }
 }
 
 public void OnPress()
 {
     _rotate = true;
 }
 
 public void OnRelease()
 {
     _rotate = false;
 }

Attach an Event Trigger to your button and have its OnPointerUp and OnPointerDown linked to OnPress() and OnRelease()

Comment
Add comment · Show 3 · 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 Cocobongo · Jan 14, 2015 at 03:51 PM 0
Share

I realize this is mostly rephrasing what @$$anonymous$$mmpies already proposed. Feel free to update your post and I'll delete my answer if this annoys you.

avatar image Mmmpies · Jan 14, 2015 at 03:55 PM 0
Share

It's fine @cocobongo you spotted the Update issue as well. It's not like I can cash karma points in anywhere.

avatar image Zer0Frost · Jan 15, 2015 at 08:50 AM 0
Share

Ah, thank you guys! I was already so close. I was lacking "private bool a" co all cunctions was crying that "a" doesnt exist in curently context.

Thank both of you :) (sorry i cant upvote you but i dont have enought reputation :C) Thanks again!

avatar image
3

Answer by Mmmpies · Jan 14, 2015 at 03:27 PM

I guess you have an OnClick event. If so then change that to onPointerDown event and call a public function in your script to set a bool to true.

Add another event onPointerUp that calls another function to set that bool to false.

Then in Update if MyBool = true rotate.

Sorry for not putting all the code I'm typing on my phone.

Should have said Add Component Event Trigger to access the pointer down and up.

EDIT:

Well @Cocobongo pretty much wrote the entire thing out for you so what's the problem?

Comment
Add comment · Show 1 · 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 Zer0Frost · Jan 15, 2015 at 08:22 AM 0
Share

-EDITED- This was written before @Cocobongo reply.

You helped alot, thanks.

avatar image
0

Answer by unity_wXpbF151lxS5ug · Sep 25, 2018 at 08:00 AM

Dear Cocobongo and Mmmpies,

Your answers have clarified my problem too, thanks. Because I used to have a similiar problem with Zer0Frost had. Your opinions worked like a charm. :) Finding answers even after 3 years is cool.

Thank you.

Comment
Add comment · Show 1 · 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 unity_zkpPDaeiSVcO_g · May 02, 2019 at 04:11 AM 0
Share

Or even four years

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

7 People are following this question.

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

Related Questions

How can I bind a custom object or variable to an UI button? 2 Answers

UI button does not press if colliding with other UI object 1 Answer

I want to make responsive homescreen. how to make responsive homescreen UI like this example ? 0 Answers

How to display UI elements in front of the gameObject? 1 Answer

Unity UI Button states 1 Answer


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