Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Showing/Hiding GUI via MouseDown

Discussion in 'Scripting' started by wilhelmscream, Oct 2, 2014.

  1. wilhelmscream

    wilhelmscream

    Joined:
    Jun 5, 2013
    Posts:
    223
    Should be a fairly simple problem, not sure why I can't get it working.

    A game object (in this specific case a tank) has a child empty that contains the unit's GUI control info (buttons, labels, health bar, etc). The idea is to have that GUI info visible when clicking on the object and going invisible when clicking elsewhere. The code I have to this point is :

    Code (JavaScript):
    1. #pragma strict
    2.  
    3. var showGUI = false;
    4. var hull : Transform;
    5. var aTextureBlitz : Texture;
    6. var aTextureAdvance : Texture;
    7. var aTextureStand : Texture;
    8. var aTextureWithdraw : Texture;
    9. var aTextureRetreat : Texture;
    10. var aTextureHealthBar : Texture;
    11. var aTextureUnit : Texture;
    12. var UnitGUISkin : GUISkin;
    13. var unitName : String;
    14.  
    15.  
    16. function Start () {
    17. }
    18.  
    19. function Update () {
    20. }
    21.  
    22. function OnMouseDown () {
    23.     showGUI = true;
    24. }
    25.  
    26. function OnGUI () {
    27.     GUI.skin = UnitGUISkin;
    28.     if (showGUI) {
    29.         GUI.Label (Rect (90, 573, 150, 22), "unitName.ToString");
    30.         if (GUI.Button (Rect (250, 574, 20, 20), aTextureRetreat)) {
    31.             SendMessageUpwards ("Retreat", hull, SendMessageOptions.RequireReceiver);
    32.         }
    33.         if (GUI.Button (Rect (270, 574, 20, 20), aTextureWithdraw)) {
    34.             SendMessageUpwards ("Withdraw", hull, SendMessageOptions.RequireReceiver);
    35.         }
    36.         if (GUI.Button (Rect (290, 574, 20, 20), aTextureStand)) {
    37.             SendMessageUpwards ("Stand", hull, SendMessageOptions.RequireReceiver);
    38.         }
    39.         if (GUI.Button (Rect (310, 574, 20, 20), aTextureAdvance)) {
    40.             SendMessageUpwards ("Advance", hull, SendMessageOptions.RequireReceiver);
    41.         }
    42.         if (GUI.Button (Rect (330, 574, 20, 20), aTextureBlitz)) {
    43.             SendMessageUpwards ("Blitz", hull, SendMessageOptions.RequireReceiver);
    44.         }
    45.         GUI.DrawTexture (Rect (360, 574, 150, 20), aTextureHealthBar, ScaleMode.ScaleToFit, true, 10.0f);
    46.         if (GUI.Button (Rect (520, 574, 20, 20), aTextureUnit)) {
    47.             SendMessageUpwards ("SelectUnit", hull, SendMessageOptions.RequireReceiver);
    48.         }
    49.     }
    50. }
    The unit has a box collider on it, I've tried setting it as both Trigger and non-trigger. The collider's bounds exceed those of the mesh itself. Clicking on the tank when in the Game screen does......... nothing. Any thoughts on where I'm screwing this up?
     
  2. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    Is your game view big enough? I tested your code with text instead of textures and couldn't see anything either, but i immediately noticed the draw calls increased. Thus i maximized it and voila.
     
  3. wilhelmscream

    wilhelmscream

    Joined:
    Jun 5, 2013
    Posts:
    223
    Yeah it's big enough. If I click the "Show GUI" box in the inspector the GUI shows up, so it's not a question of positioning or such.
     
  4. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    Re-check the collider. Something must be wrong with it. Maybe you've accidently attached it to the wrong object, because the OnMouseDown() part works fine in my scene.
     
  5. wilhelmscream

    wilhelmscream

    Joined:
    Jun 5, 2013
    Posts:
    223
    Tried it on a new object and it worked. So.... I'm assuming then that the other colliders attached to the tank are interfering. Is there a way around that without making it a massive collider?
     
  6. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    What else does your tank consist of? How many colliders, meshes etc.?
     
  7. wilhelmscream

    wilhelmscream

    Joined:
    Jun 5, 2013
    Posts:
    223
    5 meshes (hull, 2 tracks, turret, cannon).
    1 Box collider enveloping the hull and tracks.
    Turret has 1 box collider and 1 sphere collider (this one is essential to the targeting system and is set as Trigger, box colliders are not).
     
  8. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    There are probably many other solutions, but you could make the 2 box colliders listen to mouseclicks only and (either via reference or sendmessage) set the boolean in this script.
     
  9. wilhelmscream

    wilhelmscream

    Joined:
    Jun 5, 2013
    Posts:
    223
    Sorry left something out there. A capsule collider that is attached to the empty I'm trying to control (and is a child of the main hull). It's the largest of the colliders (meaning the boxes are entirely contained inside the capsule), set to trigger, and enveloped only by the turret's sphere collider. So shouldn't it be reading mouseclicks? THe boxes are for hit and physical collision detection......
     
  10. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    Well, is it the one you've tried to use as the mouse-click-listener? The sphere collider is probably catching all the mouse click events then.
    In this case, if you wanna keep the set up, another solution is to create your own click-system, which is basically the same. But you could make the raycast (from mouse position on screen to world space) ignore certain layers or hit specific layers.

    ***EDIT If you do not need to work with the sphere colliders layer, try to set it to 'Ignore Raycast'. Then you don't need to make your own system as that mouse click thingy already works with raycasts.
     
    Last edited: Oct 2, 2014
  11. wilhelmscream

    wilhelmscream

    Joined:
    Jun 5, 2013
    Posts:
    223
    Set all the other meshes to Ignore Raycast. No change.

    For some damn fool reason though..... adding a Rigidbody to the empty.....makes it work.

    Go figure.

    Thanks for the help!
     
  12. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    You did not set the big capsule to ignore raycast did you? And the object which has got that collider holds the script aswell?!
    I don't see any reason why a rigidbody should change the way it works, lol. Really strange.
     
  13. wilhelmscream

    wilhelmscream

    Joined:
    Jun 5, 2013
    Posts:
    223
    Nope, the capsule's object (and script) are not set to Ignore Raycast. Go figure.