- Home /
accessing a var in the script of one object from the script of another object
I have a Game object "AI bath tub" which has a script "AICar_Script" attached to it. I want to access several int variables that are in this script from the camera smooth follow script.
in the smooth follow script I have created a GameObject var and dragged AI bath tub to it. Now I was trying to use GetComponent to access the script and the variables in it but this doesn not work. how can I do this?
GetComponent does work. You should post your code; otherwise it's impossible to say what's wrong.
Answer by Peter G · Apr 12, 2010 at 12:35 AM
Normally I would use GetComponent(ScriptName).variable instead of static variables because static creates one instance of a variable which is sometimes helpful, but let's say you have a collection of enemies. If their health is static and one takes damage, they all take damage because they share the same variable which is usually undesirable.
Make sure you have your AICar_Script compiled before your SmoothFollow script. See this page to see the compilation order. The scripts are as simple as:
function Update () {
var smoothSpeed = GetComponent(AICar_Script).speed;
}
I am using GetComponent to get one JS scripts var in another JS script per above. I get 'myvar' is not a member of 'UnityEngine.Component' error. Any reason why, the var is there and same folder, both JS scripts.
i figured this out but, problem being you have to find the object first and then access the script using getcomponent and when using the iphone you have to type it. Not very elegant if you ask me, I should be able to just find a script and extract a var, but i guess the var could have a different value based on what object it is attached to.
Answer by MooseTrap · Apr 11, 2010 at 10:36 PM
I'm only just learning how to do this myself at the moment, but I believe that one thing to check is make sure the variables you want to access are "static var" not just "var", if you want to access them from another script. This makes them public/global so other scripts can access them.
(Correct me if I'm wrong, somebody. But I was having trouble with this, and doing this made it work for me.)
You can use GetComponent to get a script reference then access variables from that, whether the script is attached to the same GameObject or a different one. You don't have to declare the variable as static. However by declaring them as static, you will be able to access them without a script reference.
Declaring variables as static makes them unique. You should normally use GetComponent unless you have a specific reason to use static.
Answer by Ashkan_gc · Apr 26, 2010 at 04:13 AM
in your smooth follow add this on top
var o:AIbathbup;
then drag that GameObject that has the script AI bath bup on this variable of smooth follow and simply use it's variables like this
o.x=10;
you need to attach AI bath bup to a gameObject. if you want to use the GameObject itself then use
var o:GameObject;
then use
o.GetComponent(AIbathbup).x=10;
also you can store the result of getComponent in a variable and always use that variable. let's say you have a variable called s then you can write
function Awake ()
{
s=o.GetComponent(AIbathbup);
}
then in all parts of your code just use s.varname = ...
Your answer
Follow this Question
Related Questions
Script to recive varible from toher script and do something with it 1 Answer
Changing a script value 1 Answer
On var enter 1 Answer
Can't get var from other script. 1 Answer