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 and GUI

Discussion in 'Scripting' started by tobydog20, Sep 29, 2011.

  1. tobydog20

    tobydog20

    Joined:
    Jul 31, 2008
    Posts:
    71
    I would like a GUI slider to appear and stay when the user clicks on an individual game object. Then I would like the user to be able to slide the slider and the g.o.'s alpha value changes. In doing so, this is what I have so far:

    Code (csharp):
    1.  
    2. var slider : float=1;
    3. var mouseToggle : boolean;
    4.  
    5. function OnMouseDown (){
    6.     mouseToggle = !mouseToggle;
    7. }
    8.  
    9. function OnGUI () {
    10.     if (mouseToggle){
    11.     slider = GUI.HorizontalSlider( Rect(20,135,175,30), slider, 0.0, 1.0);
    12.     renderer.material.color.a = slider;
    13.     }
    14. }
    15.  
    However, the slider does not appear when I click on the appropriate g.o. I posted in UnityAnswers already (see here) and subsequently tried different Input keys, but all unsuccessfully. :(

    There are several different g.o.s, each of which will have its own slider (among other GUI elements). I would like to activate each individual game object's gui when OnMouseDown clicks on a new g.o.

    Let me know if this makes sense.
     
  2. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    Does your GameObject have a Collider on it with IsTrigger checked?
     
  3. tobydog20

    tobydog20

    Joined:
    Jul 31, 2008
    Posts:
    71
    nope. should it?
     
  4. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Whether the collider is a trigger isn't relevant.

    --Eric
     
  5. cat_ninja

    cat_ninja

    Joined:
    Jul 14, 2010
    Posts:
    197
    It just needs to be a collider, I'm not sure what's wrong with this script
     
  6. tobydog20

    tobydog20

    Joined:
    Jul 31, 2008
    Posts:
    71
    You guys rock. It worked as soon as I put the collider on it; thanks.

    I kept reading that part in the scripting reference for OnMouseDown, but b/c I haven't been using physics at all, I totally overlooked it. Just out of curiosity, why does OnMouseDown only work w/a collider or a GUIElement, rather than a straight up g.o.?
     
  7. cat_ninja

    cat_ninja

    Joined:
    Jul 14, 2010
    Posts:
    197
    I don't know why they did that, probably to give you more control over what you click rather then just it being a mesh renderer