Author Topic: Opening GUI on OnMouseDown()  (Read 9135 times)

webik150

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 36
    • View Profile
Opening GUI on OnMouseDown()
« on: September 19, 2014, 10:50:50 AM »
Hey! I've got back to unity after month, and I'm trying to make an object that would say something to you, when you click on it. I have a MessageBox panel which has background, animated sprite and a label. Then I have this script on the Item I'm clicking:
  1.         public string text;
  2.         public GameObject MessageBox;
  3.         public UILabel label;
  4.  
  5.         // Use this for initialization
  6.         void Start () {
  7.        
  8.         }
  9.        
  10.  
  11.         void OnMouseDown () {
  12.                 NGUITools.SetActive(MessageBox,true);
  13.                 label.text = text;
  14.                 Debug.Log("Hello");
  15.         }
  16. }

Also on the panel there is script:
  1. void LateUpdate () {
  2.         if (Input.anyKeyDown) {
  3.                         label.text = "";
  4.                         NGUITools.SetActive(thismessage,false);
  5.                                 }
  6.         }
to close the panel and clear the message.

When I run the game (with the panel disabled by default) and click the object, it only prints Hello!, but nothing else happens. Any ideas how to fix this?


EDIT: I would also like to know if it's possible to make a panel that would set the timescale to 0 whenever it's active. I've tried but failed.

Nicki

  • Global Moderator
  • Hero Member
  • *****
  • Thank You
  • -Given: 33
  • -Receive: 141
  • Posts: 1,768
    • View Profile
Re: Opening GUI on OnMouseDown()
« Reply #1 on: September 19, 2014, 06:17:30 PM »
I would use OnPress(bool pressed) instead of OnMouseDown, and remember to set the collider properly.

Make sure your references are set up and the UIPanel component isn't disabled instead of the gameobject. SetActive goes on the gameobject, otherwise you have to use the component.enabled. Also make sure it's not a prefab you're referencing instead of an instance in the hierarchy in MessageBox. Otherwise you have to instantiate it properly first (use NGUITools.AddChild).

As for a Panel or anything else that needs to set timescale to 0 while active, use OnEnable and OnDisable.

  1. void OnEnable()
  2. {
  3. Time.timeScale = 0f;
  4. }
  5. void OnDisable(){Time.timeScale = 1f;}
  6.  

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Opening GUI on OnMouseDown()
« Reply #2 on: September 19, 2014, 08:40:53 PM »
As Nicki pointed out, OnMouse* is a Unity function, not an NGUI function.

webik150

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 36
    • View Profile
Re: Opening GUI on OnMouseDown()
« Reply #3 on: September 20, 2014, 04:16:48 AM »
I would use OnPress(bool pressed) instead of OnMouseDown, and remember to set the collider properly.

Make sure your references are set up and the UIPanel component isn't disabled instead of the gameobject. SetActive goes on the gameobject, otherwise you have to use the component.enabled. Also make sure it's not a prefab you're referencing instead of an instance in the hierarchy in MessageBox. Otherwise you have to instantiate it properly first (use NGUITools.AddChild).

As for a Panel or anything else that needs to set timescale to 0 while active, use OnEnable and OnDisable.

  1. void OnEnable()
  2. {
  3. Time.timeScale = 0f;
  4. }
  5. void OnDisable(){Time.timeScale = 1f;}
  6.  

Hey. Ok, I should've said that before, my GameObject is a world object like chair or table. I'm trying to make some general script which I would just hook up on a game object (and set up the message, messagebox and label), and it would display the one messagebox. Here are some pictures:
This is just plain computer:

When I hover over the keyboard:

When I hover over the monitor:


For the colors I've used the OnMouseOver, so that's why I'm using OnMouseDown now. Should I really use OnPress?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Opening GUI on OnMouseDown()
« Reply #4 on: September 20, 2014, 09:32:01 AM »
It doesn't matter if it's a world object or an NGUI object.

NGUI event system works everywhere. You just need the NGUI event system script attached (UICamera) to the camera that draws the object.

So attach a UICamera to your main camera and use OnPress.

webik150

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 36
    • View Profile
Re: Opening GUI on OnMouseDown()
« Reply #5 on: September 20, 2014, 11:22:26 AM »
Damn! Still can't get it to work. The only thing I want, is to Set NGUI Panel to active when I click on an object. I already have working pause menu so why isn't this working? (I'm probably just dumb)

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Opening GUI on OnMouseDown()
« Reply #6 on: September 21, 2014, 08:52:34 AM »
My guess is that you are trying to react to your Input.AnyKeyDown in a script attached to a disabled object.

Remember, if you place this script on the disabled object, then it won't work because this script will not react to anything since it will also be disabled.

Here's a very simple script that can be placed on an active (not disabled!) object that will enable something remote:
  1. using UnityEngine;
  2.  
  3. public class ActivateOnClick : MonoBehaviour
  4. {
  5.     public GameObject targetToActivate;
  6.  
  7.     void OnClick () { targetToActivate.SetActive(true); }
  8. }

webik150

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 36
    • View Profile
Re: Opening GUI on OnMouseDown()
« Reply #7 on: September 21, 2014, 09:10:04 AM »
No! My message (the one that sets the message panel active) script is on a computer monitor (check screen).

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Opening GUI on OnMouseDown()
« Reply #8 on: September 21, 2014, 09:17:49 AM »
As long as the monitor object of yours has a collider, the camera that draws it has a UICamera, its event type is 3D World, and the Event Mask is Everything, that monitor object will be receiving NGUI events. If it doesn't, enable Debug on the UICamera and check to see what intercepts it.

webik150

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 36
    • View Profile
Re: Opening GUI on OnMouseDown()
« Reply #9 on: September 21, 2014, 02:08:35 PM »
When I put the NGUI Events script to my main camera, my OnMouseOver stops working. However OnPress(bool) gets called, but it just won't enable the message panel. I've tried almost everything, but I just can't get the panel activate.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Opening GUI on OnMouseDown()
« Reply #10 on: September 22, 2014, 10:07:49 AM »
Post your OnHover code?

webik150

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 36
    • View Profile
Re: Opening GUI on OnMouseDown()
« Reply #11 on: September 22, 2014, 10:55:22 AM »
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class ColorChangeOnHover : MonoBehaviour {
  5.  
  6.         // Use this for initialization
  7.  
  8.         Color32 initCol;
  9.         public int materialID;
  10.         public byte RED;
  11.         public byte GREEN;
  12.         public byte BLUE;
  13.         Color32 changeto;
  14.         public GameObject target;
  15.  
  16.         void Start(){
  17.                 initCol = target.renderer.materials[materialID].color;
  18.                 changeto = new Color32(RED,GREEN,BLUE,255);
  19.  
  20.         }
  21.  
  22.         void OnMouseOver () {
  23.  
  24.                 target.renderer.materials[materialID].color = changeto;
  25.  
  26.         }
  27.         void OnMouseExit(){
  28.                 target.renderer.materials[materialID].color = initCol;
  29.         }
  30.        
  31.         // Update is called once per frame
  32.         void Update () {
  33.        
  34.         }
  35. }
  36.  

But that still isn't my problem. My problem is that I can't get the NGUITools.SetActive to work with the OnMouseDown

webik150

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 36
    • View Profile
Re: Opening GUI on OnMouseDown()
« Reply #12 on: September 22, 2014, 12:57:33 PM »
Ok. I'm really sorry for bothering you with all of this. I finally found my solution. This:
  1. if (Input.anyKeyDown) {
  2.             label.text = "";
  3.             NGUITools.SetActive(thismessage,false);
  4.                 }
  5.     }
Has been called before the naked eye could see the message appear. So... I've made the message box clickable and I'm disabling it with that. :)