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 Poi7ioN · Apr 27, 2019 at 01:34 PM · buttonupdatebool

can't change bool in update function

 public void Launch()
 {
     OnclickButton = true;
     Debug.Log(OnclickButton+"On Call");
 }
 // Update is called once per frame
 void Update()
 {
     if (!HasStarted)
     {
         LockOnPaddle();
     }
 }
 
 public void LockOnPaddle()
 {
     if(OnclickButton == true)
     {
         Debug.Log(OnclickButton + "inside fucntion");
         LaunchOnClick();
     }
     else if(OnclickButton == false)
     {
         Vector2 PaddlePos = new Vector2(paddle1.transform.position.x, paddle1.transform.position.y);
         transform.position = PaddlePos + MouseToPaddlePos;
     }
 }
 
 public void LaunchOnClick()
 { 
     FindObjectOfType<Ball>().GetComponent<Rigidbody2D>().velocity = new Vector2(LaunchX, LaunchY);
     HasStarted = true;
 }

Here i'm making a ui button onclick call to Launch(), which changes the onClickbutton to true so according to function the debug is printed on console, but inside the LockOnPaddle function only the else condition is active , what am i missing here why the LaunchOnClick() function isn't getting called??? I can't get the Lockonpaddle out of update as it keeps the ball on paddle but the ball isn't launching. Please can someone help me with this ... thanks in advance.

Comment
Add comment · Show 5
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 DiegoSLTS · Apr 27, 2019 at 03:43 PM 0
Share

Are you setting the OnclickButton back to false somewhere else?

avatar image Poi7ioN DiegoSLTS · Apr 27, 2019 at 05:52 PM 0
Share

No, I've even tried using PointerDown and PointerEnter but neither of it helped ,the ball just stays on the paddle ,please can you get me out of this Problem!

avatar image DiegoSLTS Poi7ioN · Apr 27, 2019 at 06:06 PM 0
Share

You must do some debugging. Either something is changing OnclickButton to false before Update runs or HasStarted is already true then LockOnPaddle is not being called when you think it is. Use Debug.Log lines to check the values of each variable, use breakpoints, check other scripts that might be afecting the ball. The script doesn't have any obvious mistake, so there's probably something else happening.

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by JDelekto · Apr 27, 2019 at 06:24 PM

What's happening is that you are getting the event call, your OnclickButton variable is being set to true, as you expect.

However, what happens is that inside LockOnPaddle(), you are for one brief moment getting into the true condition of your if() statement, when then calls LaunchOnClick().

This is where the problem occurs. Inside LaunchOnClick(), you set the value of HasStarted to 'true'. Now, the next time your Update() gets called, the if (!HasStarted) returns false, and your LockOnPaddle() is called Nevermore.

Comment
Add comment · Show 5 · 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 Poi7ioN · Apr 27, 2019 at 06:34 PM 0
Share

So, for the 1 click event i'm setting the bool to be true is that what you are saying ? But when i change HasStarted to false it should stop running LockOnPaddle so why does the ball stay on paddle..as in the LaunchOnClick function is never called in the first place.

avatar image JDelekto Poi7ioN · Apr 27, 2019 at 06:46 PM 0
Share

I don't see anywhere in the code where HasStarted has been set to false. I made the assumption that HasStarted was never set (and by default is false). If HasStarted was true from the beginning, then LockOnPaddle() would never be called.

However, if HasStarted starts at false, then the event triggers to call Launch() and set the value of OnclickedButton to true, then LockOnPaddle() will be called, go into the if (true) condition which ultimately calls LaunchOnClick(). LaunchOnClick() then sets HasStarted to true, and then every Update() call thereafter will never call LockOnPaddle() again.

avatar image Poi7ioN JDelekto · Apr 27, 2019 at 06:59 PM 0
Share

Yeah i have a function that sets the Hasstarted to false when ball reaches the bottom collider and again gets back on paddle that works fine.wait i'll provide my previous code.

 public void InNextScene()
     {
         HasStarted = false;
     }
     void Update()
     {
         if (!HasStarted)
         {
             LockOnPaddle();
             LaunchOnClick();
         }
     }
 
     public void LockOnPaddle()
     {
         Vector2 PaddlePos = new Vector2(paddle1.transform.position.x, paddle1.transform.position.y);
         transform.position = PaddlePos + $$anonymous$$ouseToPaddlePos;
     }
 
     public void LaunchOnClick()
     {
         if (Input.Get$$anonymous$$ouseButtonDown(0) && !EventSystem.current.IsPointerOverGameObject())
         {
             FindObjectOfType<Ball>().GetComponent<Rigidbody2D>().velocity = new Vector2(LaunchX, LaunchY);
             HasStarted = true;
         }
     }

This is working but i don't want it on mouse click as i'm using ui buttons. if you can give me another solution based on button click i'm good with that :)

Show more comments

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

127 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

Related Questions

change the source image in update function? help? 1 Answer

activating multiple voids with the same name trough one line 1 Answer

Making button activate update with boolean 1 Answer

A Button, A Boolean and 2 Textures. 2 Answers

How to make on/off-like gui-button? 3 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