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 krimzondestiny · Dec 28, 2011 at 04:19 AM · togglemouseclickonmousedownonmouseup

C# MOUSE EVENT TOGGLE

I posted this in the forums when I meant to post this here. When you post in the forums, people like to post links to help files and documentation pages. I've already looked at these both and couldn't come up with an answer based solely on them, hence me posting my question.

Basically, I am trying to create a script that toggles logic based on whether a sprite (egg) is clicked or unclicked. The code I have now fires the OnMouseDown function twice on the first click, and then the OnMouseUp function twice on the first "un-click", and then nothing happens. I want it to be checked on each frame when the egg is clicked, print the string, and on each frame when the egg is released, print the other string.

 // Declare variables
 private bool isHeld;
 private int clicks;

 // Use this for initialization
 void Start () 
 {
     clicks = 0;
 }
 
 // Update is called once per frame
 void Update () 
 {
     if (isHeld)
     {
         OnMouseDown();
     }else if (isHeld == false && clicks > 1)
     {
         OnMouseUp();    
     }
 }
 
 // If player clicks egg, it is picked up
 void OnMouseDown ()
 {        
     Debug.Log("You're holding the egg!");
     clicks ++;
 }
 
 void OnMouseUp ()
 {
     isHeld = false;
     Debug.Log("You released the egg!");
 }

}

I'm thinking the double function firing is because it listens for the MouseDown and MouseUp once automatically, so that will fire it once, and then in my code I'm telling it to fire again by changing the isHeld boolean. I'm not sure logically how to work around this. Also, it only happens once instead of each time and I don't understand why.

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

Answer by Statement · Dec 28, 2011 at 04:36 AM

It fires twice since you are calling it inside Update as well. I see 4 events. One that fires when you pick up the egg. One that fire every frame while you are holding the egg. One that fires when you drop the egg. Lastly, one that fire every frame while you are not holding the egg.

You can see which parts of the code this happen by reading the debug logs.

 private bool isHeld;

 void Update() 
 {
     if (isHeld)
     {
         Debug.Log("You're holding the egg!");
     }
     else
     {
         Debug.Log("You're not holding the egg!");
     }
 }

 void OnMouseDown()
 {     
     Debug.Log("You picked up the egg!");
 
     isHeld = true;
 }

 void OnMouseUp()
 {
     Debug.Log("You dropped the egg!");
 
     isHeld = false;
 }

You can also create dedicated callbacks for these to make it crystal clear what the code should be doing and when it should be done:

 private bool isHeld;

 void Update() 
 {
     if (isHeld)
         OnEggHeld();
     else
         OnEggNotHeld();
 }

 void OnMouseDown()
 {     
     isHeld = true;
     OnEggGrabbed();    
 }

 void OnMouseUp()
 {
     isHeld = false;
     OnEggDropped();    
 }


 void OnEggHeld()
 {
     // Called while the egg is being held,
     // put code here that should happen when it is held.
 }

 void OnEggNotHeld()
 {
     // Called while the egg is NOT being held,
     // put code here that should happen when it isn't held.
 }

 void OnEggGrabbed()
 {
     // Called when the egg is grabbed,
     // put code here that should happen when it is grabbed.
 }

 void OnEggDropped()
 {
     // Called when the egg is dropped,
     // put code here that should happen when it drops.    
 }
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 krimzondestiny · Jan 02, 2012 at 08:04 PM 0
Share

This doesn't seem to work. It keeps crashing Unity every time I run this, but doesn't print an error message. I don't understand why my code wasn't working in the first place. $$anonymous$$aybe I don't understand how Update() works. I thought it was like the enter_frame works in Actionscript. I thought Update() ran the logic each frame. So like, if I had this:

 void Update()
 {
      Debug.Log("This is a new frame!");
 }

And my project was running at 24 frames/second, every second, "This is a new frame!" should be printed 24 times. However, it is only printed once. Why is this?

avatar image krimzondestiny · Jan 03, 2012 at 04:02 AM 0
Share

LOL, I figured it out. 'Collapse' needed to be toggled off. Jeez that took way too long to figure out. Ha, ha. I suck at program$$anonymous$$g.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

OnMouseDown 2 Answers

Check if an image has been clicked 1 Answer

OnMouseDown Range Problem 1 Answer

Position problem, Make my own Button 2 Answers

OnMouseButtonDown raycast 2 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