Get Inputfield integer

I have tried following several tutorials, and searching many questions of this same exact problem. However it seems Unity keeps changing things up, so nothing shows the same, nor works the same.

In my canvas I have an Inputfield, in the input field settings I set it to accept integers only. In the On End Edit(string) I placed the GameObject with the script that contains the public void functions. The functions do not show up in the Dynamic String section(as most tutorials show). They show up in the Static String section. I have tried both of these functions:

int tilesSquared;

public void StartGame(int mapSize)
    {
        tilesSquared = mapSize;
        print("mapSize was set to "+tilesSquared);
    }

    public void GetMapSize(string num)
    {
        if(int.TryParse(num, out int result)) { tilesSquared = result; }
        print("tiles squared "+ tilesSquared);
    }

And neither of them work, the printed result is always 0, no matter what number I place into the field. How did Unity setup the new way to get integer as an input? Because none of the old ways work.

You should create the variable outside the scope of the function call itself. Using Microsoft’s documentation on int.TryParse(string, out int) as an example:

// Version A: Create a temporary variable as a surrogate
int result;
if(int.TryParse(num, out result))
{
	tilesSquared = result;
}

// Version B: Use a fallback in the event the call fails
if(!int.TryParse(num, out tilesSquared))
{
	tilesSquared = tilesSquaredDefaultValue;
}

I have seen this work without declaring the input field in some tutorials, but unfortunately that’s what I had to do to get it to work:

using UnityEngine.UI;

public InputField input;
int tilesSquared;

public void GetMapSize()
{
    if(int.TryParse(input.text, out int result)) { tilesSquared = result; }
    print("tiles squared "+ tilesSquared);
}

I am not sure why this is, but you have to declare the InputField within the script, and declare the script’s function through the input fields On End Edit(string). If anyone could explain why this is, it would be appreciated. As I would like to learn how to pass variables through functions, without having to declare the UI within the script.