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 Cool Dave · Sep 10, 2012 at 08:18 PM · gameobjectsimplevardeclare

Very simple question

I have made quite a few JS scripts in Unity, like car and AI car scripts, but I have always dodged this problem.

I declare a public var 'firstVar' in one script, and attach it to a gameobject 'firstGameObject'. I have another script on another object, and I want to say, "secondVar = firstVar from firstGameObject".

How is that written? (In JavaScript)

Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by wlad_s · Sep 10, 2012 at 08:55 PM

If you make it a static var like: "static var firstVar: float = 1;" then that variable will be global and the same for all the instances of that object. Then you can reference it on any object like this: "secondVar = TheNameOFTheFirstScript.firstVar;"

If you really need it to be a public var (that is, unique for every instance of that prefab but still available from outside), then it's a bit more complicated. You need to get the first object's script as a component. For example, if your first object's name is "FirstObject", then in the script of the second object you'd have:

var myFirstObject : GameObject;

var firstObjScript : NameOfTheFirstObjectScript;

myFirstObject = GameObject.Find("FirstObject");

firstObjScript = myFirstObject.GetComponent(NameOfTheFirstObjectScript);

secondVar = firstObjScript.firstVar;

Of course, you don't need to find that object by name. You can get to it anyway you want. DragAndDrop into a gameObject variable on your second object for instance.

I hope this was clear enough :) I'm no expert at scripting but that's how I do it.

Cheers.

Comment
Add comment · Show 3 · 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 · Sep 10, 2012 at 09:06 PM 0
Share

Oh, and keep in $$anonymous$$d that the values of static vars get transferred to the next level when loaded. That's great for score and number of lives for example. But sometimes you need them to reset to initial value so you must do it in a Start function. You maybe know that already but I didn't :) so I guess it's ok to mention it should anyone else like me read this.

avatar image Cool Dave · Sep 10, 2012 at 09:16 PM 0
Share

Thanks, no I didn't actually.

avatar image Screenhog · Sep 10, 2012 at 09:20 PM 1
Share

Static variables, in certain places, can be a very good way to go (the score and lives examples are some of the best). However, static can also have some hazards (the people who make too many static variables just to avoid learning about connecting scripts together usually regret it later).

Uberweiss's method is generally how I do it as well.

avatar image
1

Answer by Screenhog · Sep 10, 2012 at 08:45 PM

You'll need to learn about accessing other components: http://docs.unity3d.com/Documentation/ScriptReference/index.Accessing_Other_Components.html

If this doesn't explain it well enough, I can go into more detail.

Comment
Add comment · 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

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

9 People are following this question.

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

Related Questions

Error Assigning GameObject To Var Inside A Class 3 Answers

How do I remove a GameObject var? 1 Answer

How to create a simple "Choose the correct answer" script 0 Answers

custom variable type? 1 Answer

using Contains(gameObject) to find and destroy a gameObject from a list 2 Answers


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