Hey people so i’m trying to make a simple math game in which you will be given a numbers and you have to decide which of >< or = is appropriate so the think here is that ><= are images the numbers are X and Y and they will be generated randomly so here is where am i right now
I’ll declare variable X and Y which are the random numbers good for now
i can compare the numbers with ><=
But how i can check if the image for ><= is true. I know it’s kinda hard to understand me so if you have any questions please ask me. All help will be appreciated.
I’m having trouble working out what you mean by “><= are images” and “check if the image for ><= is true”.
Can you post whatever code you have so far so we can get a baseline idea of what your thought process is, code-wise?
2 Likes
I think I get what you’re trying to do. Here’s a rough outline of how you’ll do it:
-
Go through the UI tutorials if you haven’t already done so.
-
Set up a canvas with a Text for the question.
-
Set up three buttons, with your images for >, <, and =.
-
Probably also set up another Text to show the result.
-
Put a script somewhere (on the Canvas is fine) that has references to all four UI objects. It will do the following:
-
Set the .text of your question Text, using string.Format to formulate that out of your random X and Y values.
-
Have a public method for each of the three buttons: CheckLesser, CheckGreater, and CheckEqual. Invoke these from the buttons.
-
In each of those methods, set the .text of the result Text according to whether they got it right.
Please do not skip step 1. Everything else depends on it.
1 Like
Thank you both that’s exactly what i was asking for i’ll be posting update when i’m home 
So this is how far i get:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class GameManager : MonoBehaviour {
public Text Math1;
public Text Math2;
void Start () {
var x = Random.Range(1, 21);
var y = Random.Range(1, 21);
var z = Random.Range(1, 21);
var w = Random.Range(1, 21);
var result1 = x + y;
var result2 = z + w;
Math1.text = x+"+"+y;
Math2.text = z + "+" + w;
if (result1 < result2)
{
print("Lesser");
}
if (result1 > result2)
{
print("Greater");
}
if (result1 == result2)
{
print("Equal");
}
}
public void CheckLesser()
{
}
public void CheckGreater()
{
}
public void CheckEqual()
{
}
}
I’m still not sure how i can check if it’s Greater Lesser or Equal? Thank you.
I’m guessing the reason you’re unsure is that you have not saved the numbers as properties of the script. You have them only as local variables, so they’re not accessible from any other method.
Change x, y, z, and w into instance variables (properly called fields). Then you can reference them in CheckLesser, etc.
1 Like