- Home /
how do i call for a string variable from another script?
lets say i have a string variable "xyz" in script 1, and in script 2 i want to use that string. I tried it by making that string a public string, like: public string acc; but how do i call for it in my second script?? because if just insert the acc as a variable, it throws an error
Answer by JathOsh · Oct 05, 2016 at 01:13 AM
So let's say I have a script called "GameMaster" Here is the code in "GameMaster"
public class GameMaster : MonoBehaviour {
public string thing = "Hello World"
}
And then i have a script called "StringGet"
public class GameMaster : MonoBehaviour {
//This will be set to the game object the script is attached to
// Named "gm" for simplicity
GameObject gm;
//Name of script in our case "GameMaster"
// Named "gms" for simplicity
GameMaster gms;
void Start () {
// Name of game object that sccript is on
gm = GameObject.Find("GameMaster");
// Now actuallly finding the script
gms = gm.GetComponent<GameMaster>();
}
void Update () {
// Now we go "Gms." and then the name of the variable
debug.log(gms.thing);
}
im still having trouble with it, are u sure the second part is ok?
im not sure, i think that my string is getting erased after certain step, because since the variable im saving comes from an input field text, i think after that step is done, it is cleared automatically so the saved string becomes empty again