Text 101 Spin Off. Math Game

To learn code better I have been taking what I learn from the course content and challenging myself with something different using similar concepts.

So after the Text 101 segment of the course I challenged myself to make a simple multiple choice math question game that generates random integers with 4 questions. Addition, subtraction, multiplication, and division. And there are two answer choices. One being the write answer, the other being incorrect. I tried creating it using scriptable objects but i could not conceptualize it properly with that method so i decided to just do it without “states” or scriptable objects. Now what I’m struggling with is that I cant seem to have the variables that I generate from a function be called out in void Update() which is where i have if statements for the input of the answer. This is my code. Any help or advise is greatly appreciated! <3

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using Random = UnityEngine.Random;

public class MathisFun : MonoBehaviour
{
    [SerializeField] Text textComponent;
    [TextArea(10, 14)] [SerializeField] string introText;
    int a;
    int r;
    int One;
    int Two;
    int True = 1;


    // Start is called before the first frame update
    void Start()
    {

        textComponent.text = introText;

    }

    // Update is called once per frame
    void Update()

    {
        if (Input.GetKeyDown(KeyCode.Alpha0))
        {

            Addition();

        }

        if (Input.GetKeyDown(KeyCode.Alpha1))
        {
            if (Addition(a) == Addition(One))
            {

                Subtraction();
                

            }
            else
            {

                WrongAnswer();
           

            }
           
        }

        if (Input.GetKeyDown(KeyCode.Alpha2))
        {
            if (Two == a)
            {

                Subtraction();

            }
            else
            {

                WrongAnswer();

            }
        }



    }

    public void Addition()
    {
        int x = Random.Range(1,10);
        int y = Random.Range(1,10);
        int a = x + y;
        int r = Random.Range(1, 10);
        int One = r;
        int Two = a;

        textComponent.text = "What is " + x + " + " + y + " equal to?" + System.Environment.NewLine + "1. " + r + System.Environment.NewLine + "2. " + a;
       
    }

    public void WrongAnswer()
    {
        textComponent.text = "YOU SUCK AT MATH!" + System.Environment.NewLine + "Press 0 to restart";
    }

    public void Subtraction()
    {
        int x = Random.Range(1, 10);
        int y = Random.Range(1, 10);
        int a = x - y;
        int r = Random.Range(1, 10);

        textComponent.text = "What is " + x + " - " + y + " equal to?" + System.Environment.NewLine + "1. " + a + System.Environment.NewLine + "2. " + r;
     
    }

}
1 Like

Hi,

Welcome to our community! :slight_smile:

In Addition(), there are a bunch of variables declared. At the top of the class, there are variables of the same name. The values in the Addition() method get assigned to the local variables in the method block. Local variables in a method block exist within the scope of the method block only. Addition() and Update() are two different scopes.

2 Likes

Hey Nina,

I really appreciate you answering my question so promptly!

So to clarify. Variables declared in a method only exist within the method but are not updated to the whole class? Is there a way for a method to change declared initially declared variables? Or should I just rethink the logic? Please advise!

Thank you,
Mac

Not only that. The variables get destroyed after the method was executed, and they get recreated when the method gets called. For this reason, we use local methods only as temporary stores.

The variables at the top of your class exist during the lifetime of the instance/object.

Remove the type (int) in front of the variables in Addition() and Subtraction() to assign the values to the instance variables. When you add a type in front of the variable, you declare a new variable.

1 Like

Got it ! I noticed that following along the course helped better understand some of these concepts.

Thanks,
Mac

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms