- Home /
Update function is not executing the bool
I am using following update function
bool Xpositive=false;
float XAxiscurrentXpos;
float izvalue;
Vector3 XAxisposition;
void Update()
{
if (Xpositive == true)
{
while (XAxiscurrentXpos <= -izvalue)
{
XAxis.transform.localPosition += Vector3.right * 0.01f * Time.deltaTime;
}
}
else
{
if (XAxiscurrentXpos > -izvalue)
{
while (XAxiscurrentXpos >= -izvalue)
{
XAxis.transform.localPosition -= Vector3.right * 0.01f * Time.deltaTime;
}
}
}} }`
Now, the Xpositive is changed in following code
public void ObjSupport_PositionArrived(string Coordinate, double Value)
{
switch (Coordinate)
{
case "X-":
{ //X-code }
break;
case "X+":
{ Xpositive = true; }
break;
case "Z-":
{ //code for Z } break;
}
}}
Problem is that when Bool Xpositive is set to true ,the IF in Update still is not executed.
You could try and debug the flow. Maybe the Update is run before the ObjSupport_PositionArrived is called and the Xpositive is reset at the end of the frame.
Answer by jmfp92 · Nov 09, 2021 at 10:31 AM
without even knowing for sure what the problem is, I'm willing to bet it's the while loop in your update function. Instead of using a while loop try doing this
void Update() {
if (Xpositive && XAxiscurrentXpos <= -izvalue){
// the rest of the stuff you said
while loops are pretty expensive and should really only be used if it is absolutely the only option
No that is not the issue. If the Xpositive become true in below function the Update should execute the if statement as condition becomes true
Ok, I'm just a little confused as to why you aren't just running the code you need to happen if Xpositive is true in the switch case "X+". Instead of setting xpositive to true and then running the code elsewhere, just run the code inside of the case example:
case "X+":
//{ Xpositive = true; }
//do the Xpositive stuff
break;
hopefully I don't sound like a jerk, I am just trying to understand
Actually i was doing this Await Task.delay() .So someone suggested me alternative of this as above and now I am having this issue
Your answer
Follow this Question
Related Questions
A Button, A Boolean and 2 Textures. 2 Answers
OnTriggerEnter if else 2 Answers
How to stop the camera in a certain position? 1 Answer
how to use bool from another script in if statment? 2 Answers
Animator if Bool 1 Answer