- Home /
Getting a variable from another object
Hi,
I'm a beginner in java script but I need to do something in Unity before I start to really dive into it.
I thought it would be simple. But after searching for hours, through the forums and manuals, I give up. It's either not what I wanted or I can't understand it. By the way, I've already made some stuff in Unity via scripting but this one is giving me headaches.
Basically I want to do this:
I have one object with a script that holds a public variable called "publicspeed"
I have another object with a private variable called "speed" and I want the "speed" of that object to be equal the "publicspeed" as soon as it instantiates.
Could someone please tell me how to do this?
Thanx!
Thanx Eric. I've read through that page many times but the problem was that I defined publicspeed as a "public var" and thought it makes it global. $$anonymous$$ixed up public and static. $$anonymous$$y bad.
Very late reply, but: public does make it global. Static and public are two different things. Variables can be public or not, and static or not. Static means there's only one instance; nothing to do with access modifiers. There seems to be this idea floating around that you have "public, private, static", but that's wrong. You have public and private (and a couple others, but never $$anonymous$$d about them now), and both public and private can be static or non-static.
For C# variable has to be public and static if one wants to CHANGE it globaly in some other script.Tryed without static and I can only acces it , but not change it.
No, that's wrong. Any public variable in any language can be changed, unless you're using properties in a way that prevents it (a getter but no setter, for example). Once again, static has nothing at all to do with access modifiers.
Answer by pigi5 · Feb 04, 2011 at 07:46 PM
In JavaScript, to make a variable accessable to other scripts, when you declare your variables, you must use the word "static" before your variable. Yours should look something like this:
static var publicSpeed : float = 0.0;
To access that variable in another script you have to use dot syntax, so "(name of the script).(name of variable)". For you this should look like this:
private var speed : float = 0.0;
function Update (){ speed = MyPublicSpeedScript.publicSpeed; }
That should work. Using the Update function, this updates your speed variable to the publicSpeed variable every frame. Of course you would replace "MyPublicSpeedScript" to whatever your script that sets the public speed variable is called. Remember, the variable name must perfectly match or else you will get an error. If your variable is named "publicspeed" then you will have to change my example to that, because I used "publicSpeed" (sorry that's just my way of formatting variable names).
Good Luck.
Thanx for this very clear and patient explanation! As I said to Eric5h5, I thought "public var" means it is public, global. And "static var" means it is static...that is - the one that cannot change :) $$anonymous$$y bad that I didn't read more about variables. Thanx a lot!
Just make - public static int myglobalvariable; and you can directly acces and change this integer variable in another script directly typing the originalscriptname.myglobalvariable=somenumber; C# explanation :)
Just note that this answer is not really correct, although it "works" (in some cases). You don't need static variables, and should not use them unless you specifically mean for only one instance to exist. Typically you would use a public (non-static) variable along with GetComponent, as described in the link I posted under the question.
Your answer
Follow this Question
Related Questions
Can't access variable from another script. 1 Answer
Modify a variable inside a game object from other object. 1 Answer
Script not responding to public variable change 1 Answer
Accessing variable from a method in another script and gameObject 2 Answers
Can I make variables visible to other scripts without making them visible in the Inspector? 1 Answer