Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Vladimir 2 · Feb 04, 2011 at 05:50 PM · objectvariableaccesspublicanother

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!

Comment
Add comment · Show 9
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Eric5h5 · Feb 04, 2011 at 05:54 PM 1
Share

Covered in the docs: http://unity3d.com/support/documentation/ScriptReference/index.Accessing_Other_Game_Objects.html

avatar image wlad_s · Feb 06, 2011 at 09:57 AM 0
Share

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.

avatar image Eric5h5 · May 09, 2013 at 11:57 PM 0
Share

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.

avatar image Dejan1 · May 10, 2013 at 12:15 AM 0
Share

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.

avatar image Eric5h5 · May 10, 2013 at 12:55 AM 0
Share

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.

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
2

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.

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image wlad_s · Feb 06, 2011 at 10:09 AM 0
Share

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!

avatar image sloopernine · May 09, 2013 at 03:36 PM 0
Share

How would the script look in C# ?

avatar image Dejan1 · May 09, 2013 at 11:41 PM 0
Share

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 :)

avatar image Eric5h5 · May 09, 2013 at 11:52 PM 2
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges