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
1
Question by zachary.collins · Apr 30, 2012 at 06:32 AM · javascriptinventoryinventory system

Having scripts interact

I am pretty new to scripting in javascript and i seem to have gotten down some of the basics. I do not understand however how to allow different scripts or variables to interact with one another. At the moment i need to make a player inventory but i do not know how to allow for the 2 scripts to interact with one another. Thanks in advance

Comment
Add comment · Show 3
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 DaveA · Apr 30, 2012 at 06:35 AM 1
Share

It's the single most popular question here (so you can Search it), and it's linked on the first page of the Scripting Reference.

avatar image fafase · Apr 30, 2012 at 06:42 AM 0
Share

Yes it is, so I made quite a long explanation with most of it so that hopefully, that will answer it once and for all.

avatar image Lo0NuhtiK · Apr 30, 2012 at 07:11 AM 1
Share

@fafase : Don't worry, it won't. Search bar is just on here for decoration.

1 Reply

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

Answer by fafase · Apr 30, 2012 at 06:41 AM

There are 3 ways to access other script members:

-|-Static, make the variable static and you can access it with the scriptname and the var name

Player.js

 static var health:int;

anywhere:

 Player.health-= damage;

Pros: Easy to use. Cons: Static means only one instance of the variable exists, meaning that if you have a static for enemy health, they all die at once. It is fine though for player health, player score, enemy counts or anything that is only once in the game.

-|-GetComponent, you can access other script with GetComponent like this:

 var other : ScriptName;
 other = gameObject.GetComponent("ScriptName");
 other.someVariable = 5;

Better is to cache if the action is used often

 var other : ScriptName;
 function Start(){
 other = gameObject.GetComponent("ScriptName");}
 
 function Update(){
 other.someVariable = 5;}

if the script belongs to another object:

 var other : ScriptName;
 function Start(){
 other = GameObject.Find("ObjectName").GetComponent("ScriptName");}
 
 function Update(){
 other.someVariable = 5;}


Pros:works for all variables and functions of any types (or almost). Cons: takes a little work and understanding at first. A little exepensive if not cached.

-|-SendMessage(), you can simply send a message to an obejct to tell him what to do.

anywhere

 GameObject.Find("OtherObject").SendMessage ("Dosomething");
 //If the script is on the same object(gameObject. is actually not compulsory)
 gameObject.SendMessage ("Dosomething");

another script

 function Dosomething(){
 //Do smg;}

if the function requires parameter, they are passed in the parenthesis as such:

 GameObject.Find("OtherObject").SendMessage ("Dosomething",10,20);

 function Dosomething(a:int, b:int):int{
 return a + b;}

Pros:quite easy to use and straightforward. Cons: Really expensive, not to be used when the action is repeated.

-|-BroadcastMessage(), works the same as SendMessage(). BM sends the message to the object and all children of the object. If they all hold a script everyone will react. SM only affect the targeted object.

Pros and cons are similar to SendMessage except cons are multiplied by the amount of children.

EDIT: I added cases for GetComponent and SendMessage when the script is on another object and Broadcast Message.

Comment
Add comment · Show 1 · 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 Bluntweapon · Apr 30, 2012 at 07:11 AM 0
Share

Don't forget Broadcast$$anonymous$$essage()

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

Inventory Drop Function Problem 1 Answer

How to optimize this script and add items imediately without grids 0 Answers

Inventory code not working [JS] 0 Answers

Is this considered bad coding 1 Answer

How i can make a script-made button interactable? 0 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