- Home /
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.
Are you setting the OnclickButton back to false somewhere else?
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!
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.
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.
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.
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.
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 :)