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 coolbird22 · Apr 16, 2014 at 06:12 PM · if-statementsstop

How do I stop an 'IF' function from running from another script ?

I need an IF function to NOT get executed altogether, if an object is colliding with another object tagged as 'Crowd'. How do I go about achieving this. Script A is the one I wish to change. Script B contains the IF function of KeyUp, that I do not wish to execute, if the object, on which Script A is attached to, is colliding with another object tagged Crowd.

Script A (attached to Object A)

         void OnTriggerEnter2D(Collider2D col)
         {
             if (col.gameObject.tag == "Crowd") 
             {
     //Add code to stop the IF statement from other script            
     Debug.Log ("Collisions are happening");
             }
         }

Script B (attached to Object B)

 void Update (){
 
         if(Input.GetKeyDown(KeyCode.Z))
         {
 Debug.Log("Key was pressed");
 }
         if(Input.GetKey(KeyCode.Z))
         {
 Debug.Log("Key is currently pressed");
 }
         if(Input.GetKeyUp(KeyCode.Z))     //Needs to stop from getting executed
         {
 Debug.Log("Key was stopped from being pressed");     //Needs to stop from getting executed
 }      }
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
0
Best Answer

Answer by jonSG · Apr 16, 2014 at 06:37 PM

Note, that in the end you don't want to use Find() and GetComponent() all the time like this. These can be cached in your a.Start()

In your "B" script, you want to do something like the following.

 public class bBehavior : MonoBehaviour {
     public bool otherIsInCroud = false;
 
     void Start () {
         otherIsInCroud = false;
     }
     
     void Update () {
         if(Input.GetKeyDown(KeyCode.Z)) { Debug.Log("Z-Key down"); }
         if(Input.GetKey(KeyCode.Z)) { Debug.Log("Z-Key held down"); }
         if(Input.GetKeyUp(KeyCode.Z))
         {
             Debug.Log("Z-Key up");
             
             if ( !otherIsInCroud ){
                    Debug.Log("Object (a) WAS NOT colliding with croud so do something");
             }
             
         }
     }
 }

In your "A" script, you want to do something like:

 void OnTriggerEnter2D(Collider2D col) {
     if (col.gameObject.tag == "Crowd")  {
         Debug.Log ("Collisions are happening");
 
         bBehavior b = GameObject.Find ("b").GetComponent<bBehavior> ();
         b.otherIsInCroud = true;
     }
 }
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 coolbird22 · Apr 19, 2014 at 08:24 PM 0
Share

That helped quite a bit. Thank you sir !

avatar image
0

Answer by fafase · Apr 16, 2014 at 06:20 PM

Add a Boolean on script B, in the collision method get the component and change the Boolean.

This Boolean is used at the top of the update to return if not the proper condition

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

21 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

Related Questions

Stop play procedure from within a script 0 Answers

Stopping a bit of script once it reaches certain number. 2 Answers

Stop momentum of a ball 1 Answer

object passes certain point in space 1 Answer

Scripts stop working in Maximize On View and testing on Android 0 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