How to reset part of a script?

Hi there :slight_smile:

I got a problem that I simply can’t figure out. I’ve searched in all related threads I could find, without luck, so now I’m turning to wiser coders :stuck_out_tongue:

I have a script in which I have to public integers, where each of their values are generated randomly on awake.
These two integers are later in the script referred to in a text that forms an equation, that the user has to solve.
If the user answers correctly, they’re told so, and if they don’t they’re told so.

What I cant figure out is how to “reroll” the random values (or the entire script), to give the user a new equation to solve, once they’ve completed the first one.

How can I approach a problem like this?
Is it possible with all of it being in the same script, or do I have to use several scripts?

Thank you for reading

I’m going to make some assumptions about your setup. I assume the user inputs a number and then hits a button. When the button is hit, you check if it’s correct, if yes, do whatever you are suppose to do. If no, run a method that rerolls two randoms and displays the new formula in your text (also calculate the new answer of course).

Your second assumption is correct, but how do you reroll the two randoms?
The 3 functions you list are the entire script, but is there a way to reset it, so that it starts over?

Move this out of Awake() into its own method, then run it whenever you want new values.

But if I do that, how do I then run it from the beginning? Without having to click or pres a button first I mean

Uhhhm… Just run that method from Awake().

2 Likes
public class MathGame : MonoBehaviour {
  int a;
  int b;

  void Awake() {
    Reroll();
  }

  public void OnClickButton() {
    //Check the answer, etc
    Reroll();
  }

  void Reroll() {
    a = Random.Range(1, 100);
    b = Random.Range(1, 100)
  }
}
1 Like

Thanks guys!