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 antiquote · Dec 16, 2013 at 02:02 AM · getcomponenthowothercant

How do I add/subtract variables between two different JavaScripts?

I know the question isn't worded very well so, I'll elaborate.

Say if the player is trying to destroy an object like a soda can for the sake of the question.

On one script the soda can has a variable for health say 100.

Then on the player script it says that if this has a tag or so of "Junk" Then deal this value of damage and subtract it from the health of the Soda can.

I'm under the impression this has something to do with broadcasting, but I tried and I have no clue.

*None of the scripts below are existent yet this is just for future reference.

SodaCan.js

 var health : int = 100;
 
 function Update(){
      curHealth;
 }

Player.js

 var junkDamage : int = 50;
 var junkObj : GameObject;
     
 function DestroyJunk (){
      if(junkObj.tag == "Junk"){
           DealDamage()?;
     
 }
     
 function DealDamge(){
     var curHealth = health - junkDamage;
 }
 


I am aware that this is no where at all accurate. I just need to know the logic behind it so I may apply it.

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
6
Best Answer

Answer by YoungDeveloper · Dec 16, 2013 at 02:12 AM

You must getcomponent from soda somehow from the player first, there are many possibilities, via Ontrigger, OnCollision, raycasting, and many other. You should choose the best method which fits your needs. But for showing you, i will be using the simplest method- that is accessing gameobject you have added via inspector.

Player.js

 var damage : int = 50;
 var sodaCanObject : GameObject; //Attach the gameobject with **SodaCan.js** in inspector
 
 function Start(){
     //First we need to get the soda script itself from sodaCanObject
     var script : SodaCan = sodaCanObject.GetComponent("SodaCan");
     script.DealDamge(damage);
 }


SodaCan.js

 var health : int = 100;
 
 public function DealDamge(damage : int){
     health -= damage; //same as health = health - damage;
 }
Comment
Add comment · Show 2 · 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 antiquote · Dec 16, 2013 at 02:22 AM 0
Share

Thank you! I appreciate for someone explaining it other than giving just the code so I may use it for future projects. If I had 15 rep to up vote you I would.

avatar image YoungDeveloper · Dec 16, 2013 at 02:48 AM 0
Share

No problem, some day you will come back, and up vote ;-)

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

17 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 avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Can't get var from other script. 1 Answer

C# GetComponent Issue 2 Answers

Use variable from another script with WWW give NULL? 1 Answer

Calling A Method From Another Script in C Sharp 3 Answers

How to make a third person free raom? 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