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 henris08 · Apr 20, 2014 at 07:31 PM · pick up object

Variable problem in Javascript.

Hello, so I`m learning Unity, and I tried writing flashlight pickup script - everything works great! Except I thought of adding a feature that does`nt let you to pick up multiple flashlight. So here is the code I need help with:

 var flight;
 flight = 0;
 function OnTriggerEnter (other : Collider)
     {
     if(flight == 0)
       {
       gameObject.collier.enabled = false;
       gameObject.renderer.enabled = false;
       gameObject.light.enabled = false;
       flight = 1;
       }
     }

Ok, so the problem is if(flight == 0) does not seem to have any influence when picking second flashlight it just picks both of them no matter what. I know that flight is being set to 1, because I tried adding Debug.Log(flight) to the script and it does set it to 1, but second flashlight still can be picked. I tried setting flight to 1 at the start of the script where flight = 0 is and then it wont pick it up, so I`m really confused. Please help.

Also until I`m here I have few more questions for you people to maybe help me with, ok here it is:

  1. As you can see I`m disabling gameObject.collider and etc instead of destroying it, because I`ll need it to re-enable in certain amount of time, so is there any shorter way to disable everyting something like gameObject.enabled = false. huh? :D (That one does not really matter, Ive just thought I should ask)

  2. How to read variable from another script? I know, I know, i`ve helluva loads of info for it, but I can`t still do it. So could u please be so kind and help me :P

Okay so as you can see I`m using variable flight in my code above (script is called Trigger.js) I`m also using the same variable in my other script (flashlight.js) and I would like to instead of creating new variable in Trigger.js to somehow extract it from flashlight.js. That sounds confusing but I hope u`ll understand me. Sorry 4 my bad English. Peace :P

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 henris08 · Apr 21, 2014 at 03:33 AM 0
Share

Thank you so much people :p

3 Replies

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

Answer by MileSplit · Apr 20, 2014 at 07:49 PM

Your problem is that your variable flight is local, meaning that is exists only in that script. When you have another flashlight ITS flight is still 0 even though your original one is 1. A good example would be a health variable. The health only describes the gameobject its attached to, not every single object. You can easily fix this with a static variable. [https://unity3d.com/learn/tutorials/modules/intermediate/scripting/statics][1] A static variable is a variable that exists only once across all scripts.

 static var flight = 0;
 function OnTriggerEnter (other : Collider)
     {
     if(flight == 0)
       {
       gameObject.setEnabled(false);
       flight = 1;
       }
     }


Accessing other scripts is easy also.

 GameObject.Find("Name of Object").GetComponent("NameOfScript").variableYouWishToAccess


Good Luck! [1]: https://unity3d.com/learn/tutorials/modules/intermediate/scripting/statics

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 henris08 · Apr 21, 2014 at 01:24 AM 0
Share

Thank you so much :) Very nicely explained, and really makes me understand what I`m doing. haha On the gameObject.setEnabled(false); I kept getting and error, so I`ve found another way if anyone else reading this needs it: gameObject.active = false;

Cheers mate :))

avatar image
0

Answer by Griffo · Apr 20, 2014 at 07:56 PM

You need to add a empty GameObject to your scene, name it something like flashLighVar, make a .js and call it enableFlashlight.js

Then your script above get a reference to the new GameObject flashLighVar

 var flashlight : GameObject;
 
 function Start(){
 
     flashlight = GameObject.Find("flashLighVar");
 }

Also in the script add a reference to the ne javascript enableFlashlight.js

 var flaslightScript : enableFlashlight;
 
 function Start(){
 
     flaslightScript = flashlight.GetComponent(enableFlashlight);
 }

Then in the enableFlashlight.js add a variable

 var allowFlashlight : boolean;

Then reference it

 if(flashlightScript.allowFlashlight){
     // allow flashlight to be picked up
     // then set it to false so you can not pick another flashlight up
     flashlightScript.allowFlashlight = false;
 }

Not tested, but should help point you in the right direction.

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 henris08 · Apr 21, 2014 at 01:25 AM 0
Share

Cheers dude, works nicely :)

avatar image
0

Answer by Burla · Apr 20, 2014 at 07:57 PM

You can achieve it this way. First declare this variable at the top of your Trigger.js

 flashlight thisFlashlight;

Then use the GetComponent() function inside your Start() function.

 void Start() {
      thisFlashlight = GetComponent<flashlight>();
 }

Now you can access the variable through thisFlashlight like the following.

 Type myVariable = thisFlashlight.variableName;

Let me know if this helped you.

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 henris08 · Apr 21, 2014 at 01:26 AM 0
Share

Thank you. It did helped indeed. :)

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

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

Related Questions

Out of order Please help 1 Answer

How can I add time to a timer on an object collision? 1 Answer

How to detect collision of a character in diffrent body parts 0 Answers

Unknown identifier: 'supportDX11'. Error Fix? 1 Answer

Help to code line of bullet like gunbound ! 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