How can I match and display two random numbers using C#?

This “should” be easy, but I have been at it all day long. Can anyone PLEASE help me?

I am trying to create two random numbers, display them on the page, and turn one red when there is a match. I must be doing something very wrong, because I can not get the page to display a match. (See this image??)

The layout is has two text ojbects called TextBox1 and TextBox2, each has the “gm.cs” linked to it. There is one button on the page which calls the three functions(getText1, getText2, and clickStart) onClick. It could be something very simple, or I could be doing this all wrong. Any help would be greatly appreciated.

Here is the code for gm.cs

using UnityEngine;
using UnityEngine.UI;
using System.Collections;

public class gm : MonoBehaviour {
	public int text1;
	public int text2;
	public Text txt1;
	public Text txt2;

	public void getText1 () {
		text1 = Random.Range (0, 9);
		Debug.Log (text1);
		txt1 = GetComponent<Text> ();
		txt1.text = (text1.ToString ("0"));
	}
	
	public void getText2 () {
		text2 = Random.Range (0, 9);
		Debug.Log (text2);
		txt2 = GetComponent<Text>();
		txt2.text = (text2.ToString("0"));	
	}

	public void clickStart() {		
		if (text1 == text2) {
                        txt1.color = Color.red;
			Debug.Log ("We have a match!");
		} else {
			Debug.Log ("nuthin");
		}
	}
}

What’s probably happening is you are calling clickStart before getText1 and getText2.

I also see problem with those lines that should not exist:

txt1 = GetComponent<Text> ();

You probably assign txt1 and txt2 to the inspector so they don’t need it.

What I propose is this, instead of calling the getText1 and 2 from again probably the button do this:

     public void clickStart() {  
         getText1();
         getText2();      
         if (text1 == text2) {
                         txt1.color = Color.red;
             Debug.Log ("We have a match!");
         } else {
             Debug.Log ("nuthin");
         }
     }

Yayyyy!!! Wow, this worked perfectly.

I added the gm.cs script to an empty object in the Heirarchy and, using the button, onClick, I called the clickStart function. The txt1 and txt2 are also attached to each textbox. (See the image below)

Thank you soooo much for taking a look at this!!