• 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 cubictoast · Oct 26, 2013 at 08:11 PM · javascripttriggerdestroydisableenable

How to Disable/Enable another Script with Triggers?

Hi.

I'm trying to have a script become enabled only when I enter a trigger, and then become disabled on trigger exit. The purpose of this is to reduce resource consumption so that my script only activates when needed. The purpose of the script being activated is to destroy an object when the user presses the "z" key, giving the illusion of picking it up. However, as stated before, I only want the script "If Z Key is pressed, destroy key" to work if I'm within the trigger. How is this possible?

Comment
Add comment · Show 2
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 sdgd · Oct 26, 2013 at 08:13 PM 0
Share

please add at least some work you've been trying to do

some script

avatar image cubictoast · Oct 26, 2013 at 08:35 PM 0
Share

$$anonymous$$y Bad Sorry.

 #pragma strict
 var IsInside : boolean = false;
 var note : GameObject;
 var HUD : GameObject;
 
 function Start () {
     
     note.SetActive(false);
     }
     
 function OnTriggerEnter () 
         {
         IsInside = true;
         note.SetActive (true);
         HUD.GetComponent(Crosshair).enabled = false;
         Debug.Log("Enable PickUp Script");
         //Where I need to Enable The Item Pick Up Script
         }
         
 function OnTriggerExit () 
     {
     IsInside = false;
     note.SetActive(false);
     HUD.GetComponent(Crosshair).enabled = true;
     Debug.Log("Disable PickU[ Script");
     //Where I need to Disable the Item Pick Up Script
     }

And The Item Script Being Toggled Enabled/Disabled

 #pragma strict
 
 var item : GameObject;
 
 function Start () 
 {
 
 }
 function Update ()
 {
     if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Z))
     {
         Destroy(item);
     }
 }
 

1 Reply

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

Answer by Zamaroth · Oct 26, 2013 at 11:16 PM

You need to store the triggered object into variable:

 function OnTriggerEnter(item : Collider)

Then you simply enable/disable the script. So your OnTrigger functions should look like this:

 function OnTriggerEnter (item : Collider) 
 {
     IsInside = true;
     note.SetActive (true);
     HUD.GetComponent(Crosshair).enabled = false;
     Debug.Log("Enable PickUp Script");
     item.GetComponent(ItemPickUpScript).enabled = true;
 }
  
 function OnTriggerExit (item : Collider) 
 {
     IsInside = false;
     note.SetActive(false);
     HUD.GetComponent(Crosshair).enabled = true;
     Debug.Log("Disable PickU[ Script");
     item.GetComponent(ItemPickUpScript).enabled = false;
 }
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 cubictoast · Oct 27, 2013 at 03:02 PM 0
Share

Haven't tested it yet, but it looks good, thanks!

avatar image Zamaroth · Oct 27, 2013 at 08:39 PM 0
Share

No problem ;)

Don´t forget to mark it as aswered if it helped you.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

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

collider trigger to disable/enable script 1 Answer

How To Keep GameObjects From Enabling Again? 2 Answers

Disable script from code 5 Answers

How to destroy one of two identical triggers? 1 Answer

How to disable/enable SSAOEffect or other specific Components? 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