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 Kaha · Nov 16, 2012 at 01:37 PM · variablefiresharinglinking

how to link 1 var between 2 scripts ?

Hi,

I want to link a variable from one script to the other.

What I try to do: The Player can pick up a gun and a bullet. When he has them, he can reload. Once the gun is loaded he can shoot.

What I have: I have two scripts:

The PickupScript (linked to Player)

 function OnCollisionEnter(collision : Collision)
 {
     if (collisionGun.gameObject.tag == "gun");        // collide with the Gun...
         audio.PlayOneShot(pickupGunSound);
         carryingGun = true;                            // ...to pick it up
         Debug.Log( "YOU HAVE THE GUN" );            // "Find the bullet"
 
     if (collisionBullet.gameObject.tag == "bullet")    // collide with the Bullet...
         audio.PlayOneShot(pickupBulletSound);
         carryingBullet = true;                        // ...to pick it up
         Debug.Log( "YOU HAVE THE BULLET" );            // "Find the gun"
 }
 
 
 function Update ()
 {
     if (carryingGun && carryingBullet)
         canReload = true;
         //Debug.Log( "YOU CAN RELOAD" );
         Reload ();
 
     if (gunLoaded)
         canFire = true;
         //Debug.Log( "YOU CAN FIRE" );
 }
 
 
 function Reload ()
 {    
     if(canReload)
     {
         if (Input.GetKeyDown(KeyCode.R))
          {   
              audio.PlayOneShot(reloadGunSound);      
              yield WaitForSeconds (reloadTime);        // wait 3 seconds
              bullets = 1;
              gunLoaded = true;                        // there is one bullet in the Gun
              Debug.Log( "THE GUN IS LOADED" );        // "The Gun is Loaded. Shoot the enemy !"   
          }
      }
 }

and the ShootScript(linked to camera)

 function Update ()
 {        
     if (Input.GetButton("Fire1"))
         Fire();
 }
 
 
 function Fire()
 {
     if(canFire==true)
     {
         canFire=false;
         FireOneShot();
     }
 }

And so I would like that when the variable gunLoaded turns true in the pickupScript, to turn true in the shoot variable as well.

Please help me to understand what went wrong and how I can fix 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

2 Replies

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

Answer by Kaha · Nov 16, 2012 at 02:39 PM

Thank you for answering.

So finally, I have made 'canFire' a static var in ShootScript:

 static var canFire = false;

and called it from the PickupScript to change it to true:

 ShootScript.canFire = true;

Hope it can help.

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 KiraSensei · Nov 16, 2012 at 02:49 PM 0
Share

No problem. You are not forced to declare your var as static.

If my answer helped you, please validate it with the check mark next to it :)

avatar image Kaha · Nov 17, 2012 at 03:30 AM 0
Share

I have tried to call it without it being a static var and got an error from the console. How do I do to use 1 variable in 2 different scripts ?

avatar image
1

Answer by KiraSensei · Nov 16, 2012 at 01:56 PM

You can have a look at : this

To be short, one of your script must know the other one and your variable should be accessible (not private).

You can give one of the scripts to the other one as a public variable (given threw the inspector) or if you prefer you can access it by script by searching it in the scene (find the game object, get the component)

Comment
Add comment · 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

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

10 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

Related Questions

Calling a variable from one script to another 1 Answer

Share variables among scripts of the same object 3 Answers

Can I make variables visible to other scripts without making them visible in the Inspector? 1 Answer

Help with this melee system js code? Max distance variable not working 1 Answer

Dynamically Changing the Character Motor Script 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