Access and Change variables from other Scripts?

Hey.

I know there are a lot of ways out there to access variables from other scripts.

A few that I know include:

static variables;

calling anotherScript;

but neither of these work for what I want to do. I have looked online in many places and haven’t found exactly what I’m looking for, and I’m sure I’m not the only one.

What needs to happen is all of these ways to access variables from other scripts should all be in one place, so if you know any good ways, leave them as an answer to this post with an example and explanation and we’ll try to make this post become gold for all newbie programmers.

Please leave the example in the following format so that it is easy to sift through all of the answers.

Example Format:

*Insert Explanation of what we need to do

*Insert code example on how to do it

*Variable names to be used are: ‘Script1’, the name of the script which contains the variable ‘string1’, and ‘Script2’, the name of the script which contains the variable ‘string2’.

Remember the goal of the answer should be to A. Show how ‘Script1’ can change it’s ‘string1’ to the value of the variable in ‘Script2’, ‘string1’. And/Or B. Show how ‘Script1’ can change the value of the variable ‘string2’ in ‘Script2’, to the value of ‘string1’ in ‘Script1’.

Make sure to do the answer in javascript. If you can also show code in C# than do both.
UpVote the best answers!

I will give you a few lines of my own code to help you. I am not sure what you mean by “access variables from other scripts should all be in one place”

GameObject.Find(“Plane”).GetComponent(forcemat).boo

Plane is the name of the gameobject that I am taking the script, forcemat from. boo is the boolean value inside of the forcemat script which is attached to the gameobject named Plane.

Another way to do this is creating a static class that contains the variables you want to access:

static class GlobalVars {

    var string1 : String;

}

Then on anyother script you can access this string by simply putting:

GlobalVars.string1

Please note that the static class script doesn’t need to be attached to any object.