- Home /
How to call Variable across scripts.
I know theres alot of stuff out there on this but I'm having trouble understanding it. If someone can show me with my scripts I'd appreciate it because I'd understand it in my context and could replicate it later. Being new I really have trouble understanding things when people use their own variables.
How do I get GameHeight and GameWidth from this script
public GameObject TileOne;
public GameObject TileTwo;
public GameObject TileThree;
public GameObject TileFour;
public GameObject TileFive;
public GameObject TileSix;
public GameObject TileSeven;
public GameObject TileEight;
public GameObject TileNine;
public GameObject TileTen;
public GameObject TileEleven;
public GameObject TileTwelve;
public GameObject TileThirteen;
public int GameWidth;
public int GameHeight;
public int Seed;
public int SeedEntered = 0;
public int WorldCreated = 0;
public Transform Player;
void OnGUI() {
if(PlayerPrefs.GetInt("Seed Entered") == 0)
{
if(GUI.Button(new Rect(10,400,1200,100), "Create the World"))
{
SeedEntered = 1;
}
}
//if(PlayerPrefs.GetInt("World Created") == 0)
//{
//if(GUI.Button(new Rect(10,600,1200,100), "Testing Something"))
//{
// Application.LoadLevel("InsideTheInn");
// SeedEntered = 1;
//}
//}
}
void Start()
{
PlayerPrefs.DeleteAll(); //Used for testing new keys, comment out this code when not in use.
WorldCreated = 0;
if(PlayerPrefs.HasKey("Player Seed"))
{
Seed = PlayerPrefs.GetInt("Player Seed");
}
else
{
Seed = Random.Range(0,10000);
PlayerPrefs.SetInt("Player Seed", Seed);
}
if(PlayerPrefs.HasKey ("Seed Entered"))
{
SeedEntered = PlayerPrefs.GetInt("Seed Entered");
}
else
{
SeedEntered = 0;
}
if(PlayerPrefs.HasKey("World Created"))
{
WorldCreated = PlayerPrefs.GetInt("World Created");
}
else
{
WorldCreated = 0;
}
}
void Update () {
if(PlayerPrefs.GetInt("Seed Entered") == 1 && PlayerPrefs.GetInt("World Created") == 1)
{
Random.seed = Seed;
}
if(SeedEntered == 1)
{
PlayerPrefs.SetInt("Seed Entered", 1);
if(WorldCreated == 0)
{
Random.seed = Seed;
PlayerPrefs.SetInt("World Created", 1);
transform.Translate(new Vector3(Random.Range(GameHeight,GameWidth),0.2f,Random.Range(GameHeight,GameWidth)));
for(int z = 0; z < GameHeight; z++)
{
for(int x = 0 ; x < GameWidth; x++)
{
float rnd = Random.value;
if(rnd <0.25f)
{
Instantiate(TileOne, new Vector3(x,0,z), Quaternion.identity);
}
else if(rnd <0.5f)
{
Instantiate(TileTwo, new Vector3(x,0,z), Quaternion.identity);
}
else
{
Instantiate(TileThree, new Vector3(x,0,z), Quaternion.identity);
}
WorldCreated = 1;
}
}
}
}
}
}
And place it in this script
public Transform player;
void Start () {
transform.Translate(new Vector3(Random.Range(GameHeight,GameWidth),0.2f,Random.Range(GameHeight,GameWidth)));
}
}
Answer by Jopan · Jun 09, 2014 at 12:48 AM
There is multiple ways that you could access those variables from another script. The "easiest" way is to make the variables static.
public static int GameWidth;
public static int GameHeight;
There is pros and cons with doing it that way and many would say not to make variables static unless you understand what you are doing. But with those variables marked static you could access them in the other script like this.
Debug.Log( ClassNameHere.GameWidth );
Debug.Log( ClassNameHere.GameHeight);
Where "ClassNameHere" is the name of the class that contains the 2 variables. So, the problem with doing it that way, GameWidth and GameHeight will no longer show up in the inspector, so you would have to set them in your script. Another problem is that there would only be 1 GameWidth and 1 GameHeight variable as opposed to 1 for each instance of your class. In the case of "GameWidth" and "GameHeight" that is probably perfectly fine since it makes since to only ever have one. But if you had an "Enemy" script, and each one had a "Health" variable, making them static would not be an option because each enemy needs it's own Health variable.
So the way to do it in most situations would be like this. You would need to declare a variable of type, "ClassNameHere" in your 2nd script.
public ClassNameHere classInstance;
Then in the inspector the variable should show up on the script component. After that simply drag the object that has your 1st script applied to it onto the classInstance slot on the object that has your 2nd script. Once that is done, you can reference those variables like so.
Debug.Log( classInstance.GameHeight );
Debug.Log( classInstance.GameWidth );
Answer by oasisunknown · Jun 09, 2014 at 12:21 AM
your looking for a get component and its a pretty lengthy explain so I will link you here first.
http://docs.unity3d.com/412/Documentation/ScriptReference/index.Accessing_Other_Components.html
but as an example that you asked for you could do something like.
in another class you would cal
myHeight = YourObject.GetComponent<ClassName>();
if I am right on that you would store a reference of the class in the new variable you created in a different script myHeight.
you could use it like this for example in the script with myHeight.
Debug.Log ("Display the game height: ") +myHeight.GameHeight;
another reference I will link you is here to a video that talks about refencing.
Is the YourObject the object that the script is attached to in the hierarchy?
So like... WorldSpawnerComponents = WorldSpawner.GetComponent<$$anonymous$$apCreationScriptV2>();
And then when I want to pull the height or the width i'd do like...
transform.Translate(new Vector3(Random.Range(WorldSpawnerComponents .GameHeight,WorldSpawnerComponents .GameWidth),0.2f,Random.Range(WorldSpawnerComponents .GameHeight,WorldSpawnerComponents .GameWidth)));
yes the yourObject is where the script was attached to.
your basically casting the reference into a variable so you can use it for later.
you only need to do the GetComponent once if possible in the start menu then you can use the variable you set it to just like any other variable.
check out the last video that I linked to you in my answer.
and the answer below is very good too.