How to communicate between scripts?

What I’ve learnt from internet. To access a variable “num” from script “xyz” of object “obj”

public GameObject obj;
public xyz script;

script = obj.getComponent<xyz>();

//script.num is your variable

But this method does not works, Am I doing it right?

Say you had a Login Script, and an RPG script, and you wanted to set the USERNAME variable in the RPG script from the Login script. This is what you would put in the Login script.

public Gamobject ObjectionYourHonour;
public string UseYourName;
public void setUsername(){
    ObjectionYourHonour.GetComponent<RPG>().username =UserYourName;
}