• 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 Dantevw1986 · Feb 25, 2012 at 03:13 AM · parametersglobalcount

Global parameter, Count int. how do i do that ?

I guess for some of you a pretty basic thing, but i don't know how to do it.

i have Javascript A, and Javascript B.

within A i have a simple int parameter called countX this count registers a event, and counts ++ for each time the event happens.

in script B, i want to be able to "read" the current value in A. And for example, be able to make it -- (so it is also -- in A)

can anyone help me out ? thanx

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by jaised · Feb 25, 2012 at 06:21 AM

In your B script put the following:

 var aScript : AScript;

 function Start() {
     aScript = gameObject.GetComponent.<AScript>(); // assuming both scripts are attached to the same GameObject
     if(!aScript)
        Debug.Log("Couldn't find ScriptA.");
 }

And then wherever you want to reference countX, just put: aScript.countX. Hopefully this helps.

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 Kleptomaniac · Feb 25, 2012 at 11:38 AM 0
Share

Adding on from what @jaised said, if your scripts are attached to different objects, add

var objWithScriptA : GameObject;

to start of script. Add gameobject with script A attached in inspector, and then ins$$anonymous$$d of aScript = gameObject.GetComponent.<AScript>(); make it

aScript = objWithScriptA.GetComponent.<AScript>();

There you go. You're all set!

avatar image Eric5h5 · Feb 25, 2012 at 12:48 PM 0
Share

It's a bit faster if you do GetComponent(AScript) rather than GetComponent.<AScript>(), plus it's easier to type.

avatar image Bunny83 · Feb 25, 2012 at 02:43 PM 0
Share

And even faster and easier is to drag&drop the script itself ;)

Ins$$anonymous$$d of

 var objWithScriptA : GameObject;

just use

 var aScript : AScript;

and drag the other gameobject onto that variable. Unity will assign the component reference of the right type if there is one. There's no need for GetComponent then.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

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

Set script global variable value priority over editor values 1 Answer

Not able to select booleans in Unity blend tree parameters 0 Answers

activeSceneChanged Event Parameters 1 Answer

Pass data from "v2f vert(appdata_vert v){}" to "float4 frag(v2f i){}" 0 Answers

How to make the Colour parameter in coroutine work with any anything with a colour variable. 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