Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Rick-APL · Dec 17, 2019 at 05:10 AM · buttonfunctiononclick

How to add an IF statement based on a button click

Sorry I'm sure this is simple. I want to create an action based on a button press in a function.

At the moment I have the following but I obviously cannot nest the created AddListener function inside void Update() and I am not sure how to modify this correctly to work as an IF statement?

 public bool accelerate = false;
 public Button throttle;
     
 void Start ()
 {
     throttle.onClick.AddListener(throttleTrue);
 }
     
 void throttleTrue()
 { accelerate = true; } // no good as is not in the loop
 
 void Update()
 { 
 accelerate = false; // reset button click
 // Need IF section here to pull button click
 If (accelerate) 
 { Do stuff.. }
 }
 
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
1

Answer by vantjac · Dec 17, 2019 at 05:45 AM

Since you're setting accelerate to false at the start of update, the if condition will never be true. Try setting accelerate to false inside the if block, after you know it's true. Or at the end of update.

Comment
Add comment · 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
0

Answer by ethanol3310 · Dec 17, 2019 at 05:53 AM

1) put public in front of void update. 2) Set an onclick event for the UI button and assign the Update variable.

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 vantjac · Dec 17, 2019 at 05:56 AM 0
Share

This will not work, the update function is already being called every frame by Unity.

avatar image
0

Answer by Rick-APL · Dec 18, 2019 at 01:22 AM

Thanks guys. So updating this, how would I create an IF condition based on the AddListener?

  public bool accelerate = false;
  public Button throttle;
      
  void Start ()
 {
      throttle.onClick.AddListener(throttleTrue);
 }
      
  public void Update()
  { 
 >> IF (throttleTrue) <<
 accelerate = true;
 { Do stuff.. }
 accelerate = 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 unity_ek98vnTRplGj8Q · Dec 18, 2019 at 03:42 PM 0
Share
 public bool accelerate = false;
 public Button throttle;
 
 void Start () {
     throttle.onClick.AddListener (ThrottleClick); //onClick will only occur once
 }
 
 public void Update () {
     if(accelerate){
         //Do stuff
     }
     accelerate = false;
 }
 
 public void ThrottleCallback(){
     accelerate = true;
 }
avatar image unity_ek98vnTRplGj8Q · Dec 18, 2019 at 03:45 PM 1
Share

This will only happen once when you click the button. If you have a throttle button, maybe you are looking for something more like this

 public bool accelerate = false;
 public Button throttle;
 
 void Start () {
     throttle.onPointerDown.AddListener (ThrottleDown); 
     throttle.onPointerUp.AddListener (ThrottleUp); 
 }
 
 public void Update () {
     if(accelerate){
         //Do stuff
     }
 }
 
 public void ThrottleDown(){
     accelerate = true;
 }
 
 public void ThrottleUp(){
     accelerate = false;
 }

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

143 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Android, function. 1 Answer

Instantiate on click if enough gold / ressources 2 Answers

My button does not assign any variable value,My OnClick button works with Debug.Log, but doesnt assign any values it should 0 Answers

determine the minimum time for the player to hold the button 1 Answer

LoadLevel easy i know, but not working. 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