cannot return private string variable through a public get function?

public class MapTile : MonoBehaviour {
private string mapName;

public void SetMapName(string t_mapName){
mapName = t_mapName;
}

public string GetMapName(){
	return mapName;

}
}

Basically, i have a class like this. I had the string set somewhere else, and expected to be able to get it through the get function, but i couldn’t.

However, when i changed the string from private to public it turned out working. Why is that? I just want to keep this string variable private. Please help!

You already have mapName so why are you running another method to get it. That is kind of redundant.

string mapName = "";

public void SetMapName(string _name)
{ 
    mapName = _name;
}

Debug.Log(mapName);