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 Punkjim420 · Aug 24, 2012 at 04:47 PM · javascriptvariablesmagic

Can 2 scripts attached to one object communicate with each other?

In UnityScript.

ok say i have magic points in my players status script(health, magic, strength, expirience etc), would i be able to write to ask my players status script what the magic variable current is from inside my abilities script? i want to search for my magic points amount, and if i have enough, allow the abilities coded inside of my abilities script. BOTH SCRIPTS are on ONE OBJECT.

i just dont know if i have to actually "search" for the variable "magicPoints" or if itll already know, sense both scripts are parts of a single rigidbody object.

if i can do this, what methods are possible?

Comment
Add comment · Show 1
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 Bunny83 · Aug 24, 2012 at 09:56 PM 0
Share

@Fattie: I've often deleted whole discussions which of course were off topic after we cleared the facts. Every comment with useful information will be kept ;)

2 Replies

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

Answer by Bunny83 · Aug 24, 2012 at 04:50 PM

Sure, if the abillities script needs to access those variables more than once, you should setup the reference in start:

 private var playerStatus : NameOfYourPlayerStatusScript;
 
 function Start()
 {
     playerStatus = GetComponent(NameOfYourPlayerStatusScript);
 }
 
 function Update()
 {
     if (playerStatus.magic > 20)
     // [...]
 }
Comment
Add comment · Show 5 · 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 Fattie · Aug 24, 2012 at 05:00 PM 0
Share

You know, I usually do this in Awake()

(because "I might need to use it in Start()," or, someone else might need to use it early),

but I don't know if I'm making a morally sound decision.

avatar image Bunny83 · Aug 24, 2012 at 05:18 PM 0
Share

Sure, Awake is fine in most cases, but in some it won't work. For example when you attach the script manually with AddComponent at runtime. Awake is called even before the AddComponent call returns. If both scripts need to cross reference each other it won't work in Awake. I usually prefer Start but, like you said, it depends on your initialization philosophy and general structure.

avatar image Punkjim420 · Aug 24, 2012 at 05:26 PM 0
Share

thanks, this is a great answer.

Its one of the few that not only showed what i should code to get the result i want, but it also is simple enough to understand what the coding means and that allows for other usage when attempting to make something other than this specific thing. most tutorials dont explain what they are doing. its more like "do this and this will happen."

avatar image Fattie · Aug 24, 2012 at 05:31 PM 0
Share

Good grief, don't give him a big head! :-)

avatar image Fattie · Aug 24, 2012 at 05:32 PM 0
Share

"Awake is called even before the AddComponent call returns" .. indeed, a great point.

In a word, if you are attaching scripts at runtime (perhaps at set-up-time), it won't work in Awake().

Thanks !

avatar image
0

Answer by Mander · Aug 24, 2012 at 04:57 PM

 1.global variables.(i don't recommend this)
 
 declare as static so u can use it.
 
 for instance script, name: script1,script2
 
 script 1: variable  declaration
 
 public static int magicDamage;
 
 script 2: variable usage
 
 damage = script1.magicDamage * spellPower;
 
 ////////////////
 
 2.get as components 
 script 1: variables public int magicDamage = 20;
 
 script 2: int magicDamage;
 
 declare variables normaly not static and just ask for the variables with GetComponent
 and use them
 
 magicDamage = GetComponent<script2>().magicDamage;
 
 damage = magicDamage * spellPower;
Comment
Add comment · Show 5 · 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 Fattie · Aug 24, 2012 at 05:01 PM 0
Share

you probably wouldn't really do this dude. you just access the other class.

avatar image Bunny83 · Aug 24, 2012 at 05:09 PM 0
Share

Also, unlike most other questions, this one clearly says in UnityScript. I'm also a C# user, but when you post examples, at least add what language it's written in, or it wouldn't be much help for new users.

Also it seems you turned your whole answer into a code block. That also doesn't help to understand what you're doing there. Either only marks code as code or turn your text into inline comments.

avatar image Mander · Aug 24, 2012 at 05:20 PM 0
Share

well. im just tryin to help. it doesn't hurt to use the doc to "translate" from c# to unityscript. helping ppl too much is bad too, else they will try to get all their scripts from UAnswers. just saying

avatar image Punkjim420 · Aug 24, 2012 at 05:41 PM 1
Share

Thanks for your input, its always great help to find a coder who explains what they are doing rather than just pasting code snippets. In method 2 you used GetComponent to access variables from the script you searched for. I understand, and prefer GetCompnent and shall use that method.

avatar image Mander · Aug 24, 2012 at 05:51 PM 0
Share

my pleasure man. im just glad to help and i belive that if i explain the code somehow ppl will get the idea and try to implement it them selves in other scripts rather than ask again.

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

11 People are following this question.

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

Related Questions

Operator '-' cannot be used ... error 1 Answer

How to access variables between scripts in unity JavaScript? 3 Answers

How do I access a script variable from a class defined within that script? 1 Answer

variable scope problem 1 Answer

Script access (without script name) 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