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 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.

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

gameobjects don't add to count when destroyed 2 Answers

Global update function for network 1 Answer

Touch point to world coordinate and local coordinate 1 Answer

How to rotate an object around the world axis but not on the local axis? 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