Skip to main content
improved code snippet (added semi colon, added log statement)
Source Link
disc_code22
  • 418
  • 2
  • 10

From unity documentation: "FixedUpdate functions will not be called when timeScale is set to zero.".

So setting timescale to 0 would prevent execution of fixedUpdate, but it will also pause the entire physics simulation across your entire game, which I don't think is the desired behavior if I am interpreting your question correctly. I think this is a bit of an xy problem.

If you have code that you want to only execute if certain conditions are met you should use an if-statement. You mentioned in your question that an if-statement hadn't worked before, but this is its exact use case, and it should work if implemented properly. In this case you want to evaluate the "shouldExecute" boolean and then execute or not depending on the boolean value.

I would also recommend taking the code inside of fixed update and extracting it into another method to improve maintainability and modularity of the code.

Here is an example of how to implement the if-statement:

bool shouldExecute = false;

 private void function setRandomColor(){
    //.....Do stuff in here
    console.log("setting random color");
  }

 private void FixedUpdate(){
   if(shouldExecute == true){
       setRandomColor();
     }
  }

 private void OnMouseDown(){
        // Mouse event has been changed to toggle the boolean rather than 
        // only turn it off
        shouldExecute = !shouldExecuteshouldExecute;
  }

From unity documentation: "FixedUpdate functions will not be called when timeScale is set to zero.".

So setting timescale to 0 would prevent execution of fixedUpdate, but it will also pause the entire physics simulation across your entire game, which I don't think is the desired behavior if I am interpreting your question correctly. I think this is a bit of an xy problem.

If you have code that you want to only execute if certain conditions are met you should use an if-statement. You mentioned in your question that an if-statement hadn't worked before, but this is its exact use case, and it should work if implemented properly. In this case you want to evaluate the "shouldExecute" boolean and then execute or not depending on the boolean value.

I would also recommend taking the code inside of fixed update and extracting it into another method to improve maintainability and modularity of the code.

Here is an example of how to implement the if-statement:

bool shouldExecute = false;

 private void function setRandomColor(){
    //.....Do stuff in here
  }

 private void FixedUpdate(){
   if(shouldExecute == true){
       setRandomColor();
     }
  }

 private void OnMouseDown(){
        // Mouse event has been changed to toggle the boolean rather than 
        // only turn it off
        shouldExecute = !shouldExecute
  }

From unity documentation: "FixedUpdate functions will not be called when timeScale is set to zero.".

So setting timescale to 0 would prevent execution of fixedUpdate, but it will also pause the entire physics simulation across your entire game, which I don't think is the desired behavior if I am interpreting your question correctly. I think this is a bit of an xy problem.

If you have code that you want to only execute if certain conditions are met you should use an if-statement. You mentioned in your question that an if-statement hadn't worked before, but this is its exact use case, and it should work if implemented properly. In this case you want to evaluate the "shouldExecute" boolean and then execute or not depending on the boolean value.

I would also recommend taking the code inside of fixed update and extracting it into another method to improve maintainability and modularity of the code.

Here is an example of how to implement the if-statement:

bool shouldExecute = false;

 private void function setRandomColor(){
    //.....Do stuff in here
    console.log("setting random color");
  }

 private void FixedUpdate(){
   if(shouldExecute == true){
       setRandomColor();
     }
  }

 private void OnMouseDown(){
        // Mouse event has been changed to toggle the boolean rather than 
        // only turn it off
        shouldExecute = !shouldExecute;
  }
Added mention of xy problem + link
Source Link
disc_code22
  • 418
  • 2
  • 10

From unity documentation: "FixedUpdate functions will not be called when timeScale is set to zero.".

So setting timescale to 0 would prevent execution of fixedUpdate, but it will also pause the entire physics simulation across your entire game, which I don't think is the desired behavior if I am interpreting your question correctly. I think this is a bit of an xy problem.

If you have code that you want to only execute if certain conditions are met you should use an if-statement. You mentioned in your question that an if-statement hadn't worked before, but this is its exact use case, and it should work if implemented properly. In this case you want to evaluate the "shouldExecute" boolean and then execute or not depending on the boolean value.

I would also recommend taking the code inside of fixed update and extracting it into another method to improve maintainability and modularity of the code.

Here is an example of how to implement the if-statement:

bool shouldExecute = false;

 private void function setRandomColor(){
    //.....Do stuff in here
  }

 private void FixedUpdate(){
   if(shouldExecute == true){
       setRandomColor();
     }
  }

 private void OnMouseDown(){
        // Mouse event has been changed to toggle the boolean rather than 
        // only turn it off
        shouldExecute = !shouldExecute
  }

From unity documentation: "FixedUpdate functions will not be called when timeScale is set to zero.".

So setting timescale to 0 would prevent execution of fixedUpdate, but it will also pause the entire physics simulation across your entire game, which I don't think is the desired behavior if I am interpreting your question correctly.

If you have code that you want to only execute if certain conditions are met you should use an if-statement. You mentioned in your question that an if-statement hadn't worked before, but this is its exact use case, and it should work if implemented properly. In this case you want to evaluate the "shouldExecute" boolean and then execute or not depending on the boolean value.

I would also recommend taking the code inside of fixed update and extracting it into another method to improve maintainability and modularity of the code.

Here is an example of how to implement the if-statement:

bool shouldExecute = false;

 private void function setRandomColor(){
    //.....Do stuff in here
  }

 private void FixedUpdate(){
   if(shouldExecute == true){
       setRandomColor();
     }
  }

 private void OnMouseDown(){
        // Mouse event has been changed to toggle the boolean rather than 
        // only turn it off
        shouldExecute = !shouldExecute
  }

From unity documentation: "FixedUpdate functions will not be called when timeScale is set to zero.".

So setting timescale to 0 would prevent execution of fixedUpdate, but it will also pause the entire physics simulation across your entire game, which I don't think is the desired behavior if I am interpreting your question correctly. I think this is a bit of an xy problem.

If you have code that you want to only execute if certain conditions are met you should use an if-statement. You mentioned in your question that an if-statement hadn't worked before, but this is its exact use case, and it should work if implemented properly. In this case you want to evaluate the "shouldExecute" boolean and then execute or not depending on the boolean value.

I would also recommend taking the code inside of fixed update and extracting it into another method to improve maintainability and modularity of the code.

Here is an example of how to implement the if-statement:

bool shouldExecute = false;

 private void function setRandomColor(){
    //.....Do stuff in here
  }

 private void FixedUpdate(){
   if(shouldExecute == true){
       setRandomColor();
     }
  }

 private void OnMouseDown(){
        // Mouse event has been changed to toggle the boolean rather than 
        // only turn it off
        shouldExecute = !shouldExecute
  }
Source Link
disc_code22
  • 418
  • 2
  • 10

From unity documentation: "FixedUpdate functions will not be called when timeScale is set to zero.".

So setting timescale to 0 would prevent execution of fixedUpdate, but it will also pause the entire physics simulation across your entire game, which I don't think is the desired behavior if I am interpreting your question correctly.

If you have code that you want to only execute if certain conditions are met you should use an if-statement. You mentioned in your question that an if-statement hadn't worked before, but this is its exact use case, and it should work if implemented properly. In this case you want to evaluate the "shouldExecute" boolean and then execute or not depending on the boolean value.

I would also recommend taking the code inside of fixed update and extracting it into another method to improve maintainability and modularity of the code.

Here is an example of how to implement the if-statement:

bool shouldExecute = false;

 private void function setRandomColor(){
    //.....Do stuff in here
  }

 private void FixedUpdate(){
   if(shouldExecute == true){
       setRandomColor();
     }
  }

 private void OnMouseDown(){
        // Mouse event has been changed to toggle the boolean rather than 
        // only turn it off
        shouldExecute = !shouldExecute
  }