Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Event Handler Logic

Discussion in 'Scripting' started by Jessy, Aug 27, 2007.

  1. Jessy

    Jessy

    Joined:
    Jun 7, 2007
    Posts:
    7,325
    Is there some way to combine event handler functions logically? I want the same thing to happen in each of these instances:

    Code (csharp):
    1. function OnMouseEnter() {if (Input.anyKey)}
    or

    Code (csharp):
    1. function OnMouseOver() {if (Input.anyKeyDown)}
    I can just copy the code from one function to the other, but I suspect there may be a better way.
     
  2. User340

    User340

    Joined:
    Feb 28, 2007
    Posts:
    3,001
    Maybe just create your own function.

    Code (csharp):
    1. function OnMouseEnter ()
    2. {
    3.       MyFunction ();
    4. }
    5.  
    6. function OnMouseOver ()
    7. {
    8.       MyFunction ();
    9. }
    10.  
    11. function MyFunction ()
    12. {
    13.       if (Input.anyKeyDown)
    14.       {
    15.           // Do something.
    16.       }
    17. }
     
  3. Jessy

    Jessy

    Joined:
    Jun 7, 2007
    Posts:
    7,325
    That was really close to what I needed. Thanks a bunch! Here's the meat of what I came up with (It would be nicer to just use a "||"):

    Code (csharp):
    1. function OnMouseEnter() {
    2.     if (Input.anyKey){
    3.         myFunction();
    4.     }
    5. }
    6.  
    7. function OnMouseOver() {
    8.     if (Input.anyKeyDown){
    9.         myFunction();
    10.     }
    11. }      
    12.  
    13. function myFunction(){
    14.     //do something
    15. }