Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 DiamondMC102 · Sep 17, 2017 at 03:10 PM · scripting problem2d game2d-platformerdisablemethod

How can I disable a Method.

So I made a script and (we’ll call this script attack_02), and in that script, I made a method…

 void OnTriggerEnter2D(Collider2D coll){
 if (coll.gameobject.tag == "Enemy") {
 Destroy (coll.gameObject);
 }
 }

This method destroys the Enemy gameobject if the Player’s BoxCollider2D and the Enemy’s BoxCollider2D collided. I decided that I wanted this method to only happen if we hold down the H key, so I did that in another script called “attack_01”. But I enabled/disabled the entire attack_02 script.

 1. if (Input.GetKey(KeyCode.H))
 2. {
 3. attack_02.enabled = true;
 4. }
 5. else
 6. {
 7. attack_02.enabled = false;
 8. }
  

I thought I would work and the BoxCollider2D for the Player wouldn’t destroy the Enemy if they collided when the H isn’t held down. But I did, I feel as though me disabling/enabling the script has no effect on the the method(that’s just my theory). By the way I disabled the attack_02 script in the start method ,with this line of code.

 19. attack_02.enabled = false;

My Question is pretty the same as the topic question, How can I disable a Method, or if you have any better solutions please help.

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

Answer by fafase · Sep 17, 2017 at 03:28 PM

 bool keyPressed = false;
 void Update(){
     if (Input.GetKey(KeyCode.H)){ keyPressed = true; }
     else if (Input.GetKeyUp(KeyCode.H)) { keyPressed = false;} 
 }
 
 void OnTriggerEnter2D(Collider2D coll){
         if (coll.gameobject.tag == "Enemy" && keyPressed) {
               Destroy (coll.gameObject);
  }

this uses a boolean to set when key is down. Then in the collision, you check whether it is true/false. You can't really check input in Collision as you could miss some hit.

Comment
Add comment · Show 4 · 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 Bunny83 · Sep 17, 2017 at 05:10 PM 0
Share

Uhm checking Get$$anonymous$$ey should work inside OnTriggerEnter2D. It just returns the state of the key, no matter where you check it. So you could simply do

 void OnTriggerEnter2D(Collider2D coll){
     if (coll.gameobject.tag == "Enemy" && Input.Get$$anonymous$$ey($$anonymous$$eyCode.H) ) {
         Destroy (coll.gameObject);
 }

Of course things like Get$$anonymous$$eyDown or Get$$anonymous$$eyUp won't work in OnTriggerEnter but it would work in OnTriggerStay.

avatar image fafase Bunny83 · Sep 17, 2017 at 07:01 PM 0
Share

It does work in this case but since Input and Physics are not aligned it is often better not to start putting input in physics method. That is for those checking Up/Down.

avatar image DiamondMC102 fafase · Sep 18, 2017 at 12:08 AM 0
Share

@fafase thanx but your script isn't what I was looking for

avatar image DiamondMC102 Bunny83 · Sep 18, 2017 at 12:07 AM 0
Share

@Bunny83 thanx your script help the most.

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

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

Related Questions

What happened to Line Renderer! 1 Answer

Add audio. 1 Answer

How can I flip only my 'Player' gameobject, and l leave it's child object alone? 2 Answers

Don't Flip Child, Only Parent. 0 Answers

Jumping isn't smooth while going up 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