- Home /
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!
"...but neither of these work for what I want to do."
What do you want to do in your current project that your existing techniques don't solve?
Really I just think we should consolidate all the techniques to one post. But what I want to do is change a variable of an instantiated object from another instantiated object that gets the variable from another object's script. If that makes any sense XD
Answer by emc233 · Dec 28, 2013 at 05:40 PM
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.
Answer by thaiscorpion · Dec 28, 2013 at 05:44 PM
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.
Your answer
Follow this Question
Related Questions
Can someone help me fix my Javascript for Flickering Light? 6 Answers
Setting Scroll View Width GUILayout 1 Answer
Change all value of variable 1 Answer
Gun Script Help 2 Answers
Having both mouse orbit and smooth follow work at the same time? 0 Answers