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;
}
}