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

OnMouseDown not working..

Discussion in 'Scripting' started by nickavv, Sep 30, 2007.

  1. nickavv

    nickavv

    Joined:
    Aug 2, 2006
    Posts:
    1,801
    I have four objects, each with box colliders, and a 3D Text Mesh. I am using this same script on all of them.

    Code (csharp):
    1.  
    2. var powerlev = 0;
    3.  
    4. function OnMouseDown () {
    5.     if (powerlev == 1) {
    6.         LevelUp.mode = "Sleepy";
    7.     }
    8.     if (powerlev == 2) {
    9.         LevelUp.mode = "Brisk";
    10.     }
    11.     if (powerlev == 3) {
    12.         LevelUp.mode = "Rapid";
    13.     }
    14.     if (powerlev == 4) {
    15.         LevelUp.mode = "Insanity!!";
    16.     }
    17.     Application.LoadLevel ("BasicArea");
    18. }
    19.  
    But only the last button will work. I'm setting the powerlev to its different numbers on the object in the editor. Why don't the first three objects work? Thanks!
     
  2. Foxxis

    Foxxis

    Joined:
    Jun 27, 2006
    Posts:
    1,108
    A doubt it's the mouseup per se? Have you checked and double checked any differences between the objects?
     
  3. nickavv

    nickavv

    Joined:
    Aug 2, 2006
    Posts:
    1,801
    Yes, they're EXACTLY the same except for the text in their Text Mesh and the powerlevel variable.
     
  4. Foxxis

    Foxxis

    Joined:
    Jun 27, 2006
    Posts:
    1,108
    - Anything in the way?
    - Are you setting timescale?
     
  5. nickavv

    nickavv

    Joined:
    Aug 2, 2006
    Posts:
    1,801
    o_O whoops. There was another collider on an invisible object in front of those ones. Thanks for trying to help anyways! n_n
     
  6. benblo

    benblo

    Joined:
    Aug 14, 2007
    Posts:
    476
    Just a remark: if you're gonna pile "ifs" like that, use else in between each of them. This way if your value is 1, the first if is true, and you don't evaluate all the other conditions thanks to the else.
    Or, use a case, which is made exactly for what you're doing (but the syntax is ugly).

    That should make your code faster by about 0.000000000000001% ;), but hey, never hurts...