- Home /
accesing variable from other script
I have been trying to access variable speed (and change it) which is in script A from script B.
i read threads on forums and on the internet, but nothing seems to work.
Example from: https://docs.unity3d.com/Documentation/ScriptReference/GameObject.GetComponent.html
function Update () {
// To access public variables and functions
// in another script attached to the same game object.
// (ScriptName is the name of the javascript file)
var other : ScriptName = gameObject.GetComponent(ScriptName);
// Call the function DoSomething on the script
other.DoSomething ();
// set another variable in the other script instance
other.someVariable = 5;
}
sorry for late response. i would like to access variable from another script, which is not on same GameObject. The upper solution would work fine if both scripts were attached on same GameObject.
Answer by maroltl · Mar 20, 2014 at 06:13 PM
I finally got it working with
GameObject.Find("diger").GetComponent(movement).speed = 5;
You have to accept anyone's answer if its help to you :) Not yours
Answer by sriram90 · Mar 20, 2014 at 05:48 PM
Using GetComponent() you can achieve it.
If you're using C# make your variable as Public static int (or) some other data type, and access from another script.
Answer by Jester0311 · Mar 20, 2014 at 06:42 PM
I despise Java but i think what your looking for is a global or public variable in which i think is declared above your start() and update() blocks this should be able to be accessed from another script you may need to use .find method when calling it.
EDIT
Found this tutorial that may help.
Your answer
Follow this Question
Related Questions
How would I change the value of one prefab's variable via script without changing all of them? 1 Answer
Error in accessing variables from another script 1 Answer
Get access and Flip between different variables from GameManager 0 Answers
Accessing variables from scripts issues 1 Answer
reference to script not working 1 Answer