Author Topic: UiButtonMessage help: sending name of gameoject?  (Read 17367 times)

2cat

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 10
    • View Profile
UiButtonMessage help: sending name of gameoject?
« on: February 11, 2013, 09:46:31 AM »
Hey everybody, I'm an artist that's trying some C# scripting for a little side project and I'm having some problems.

Its becoming somewhat of a hastle to make a different function for each button. So what I want to do is make one function, OnButton, in my case and have that handle all button click requests.

What I want it to do is to go through a switch statement that checks the name of the button, "Select_Button", "Play_Button", etcetera.

But I've run into a problem that I can't seem to figure out just yet. if I put a debug.log in the Send() function of UiButtonMessage: Debug.Log("Debugging the clicked Button: " + gameObject); it returns the correct value. Eg. Select_Button or Play_Button or other button names.


As of now this is my switch statement:

  1.         public void OnButton()
  2.         {
  3.                 switch(somethingShouldGoHere)
  4.                 {
  5.                         case "Left_Button":
  6.                                 Debug.Log ("pressed left button");
  7.                         break;
  8.                         case "Right_Button":
  9.                                 Debug.Log ("pressed Right button");
  10.                         break;
  11.                         case "Select_Button":
  12.                                 Debug.Log ("pressed Select button");
  13.                         break;
  14.                 }
  15.         }

I've tried to make a string variable in the script that holds my OnButton function and have the Send() function write the name of the gameobject in that variable but it returns as a nullreference exception.

Although scripting is kinda fun I feel I'm in way over my head and it took me about 3 hours getting nowhere before I went to sleep. I hoped getting back to it the next day would help but it didn't.

PS I'm using the free version if that matters

redhawk

  • Newbie
  • *
  • Thank You
  • -Given: 1
  • -Receive: 0
  • Posts: 34
    • View Profile

2cat

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 10
    • View Profile
Re: UiButtonMessage help: sending name of gameoject?
« Reply #2 on: February 11, 2013, 11:30:02 AM »
Thanks, I saw your post earlier when I was searching for a possible solution. One of the problems is that it's not in C# and I don't fully understand what is going on so I'd need further explanation.

redhawk

  • Newbie
  • *
  • Thank You
  • -Given: 1
  • -Receive: 0
  • Posts: 34
    • View Profile
Re: UiButtonMessage help: sending name of gameoject?
« Reply #3 on: February 11, 2013, 11:36:40 AM »
You should do like I did, declare your Button Objects. 

Something like

public GameObject UIButton1;
...Button2
...Button3

void OnClick(){
    if(UIButton1) //I'm not sure if this should return boolean or what...
}

redhawk

  • Newbie
  • *
  • Thank You
  • -Given: 1
  • -Receive: 0
  • Posts: 34
    • View Profile
Re: UiButtonMessage help: sending name of gameoject?
« Reply #4 on: February 11, 2013, 11:39:33 AM »
Basically that for() statement or your switch() statement should be basically the same.  Instead of function itemClicked() it would be something like public void itemClicked(GameObject myObject){}

redhawk

  • Newbie
  • *
  • Thank You
  • -Given: 1
  • -Receive: 0
  • Posts: 34
    • View Profile
Re: UiButtonMessage help: sending name of gameoject?
« Reply #5 on: February 11, 2013, 12:01:35 PM »
Okay, I updated my post to include a C# version.  Mind you, I don't know C#, but the code I posted works.  Maybe I need to learn C#.


  1. sing UnityEngine;
  2. using System.Collections;
  3.  
  4. public class UICameraTestingC : MonoBehaviour {
  5.        
  6.         public GameObject[] targetButtons;
  7.        
  8.         // Update is called once per frame
  9.         void Update () {
  10.                 foreach(GameObject me in targetButtons){
  11.                         //UIEventListener.Get(targetButton).onClick=ItemClicked;
  12.                         UIEventListener.Get(me).onClick=ItemClicked;
  13.                 }
  14.         }
  15.         public void ItemClicked(GameObject btnObj)
  16.         {
  17.        
  18.                         Debug.Log(btnObj.name+" was pressed");
  19.         }
  20. }

2cat

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 10
    • View Profile
Re: UiButtonMessage help: sending name of gameoject?
« Reply #6 on: February 11, 2013, 12:06:59 PM »
Ok, lets break it down because I'm not sure if I get all this.

  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class UICameraTestingC : MonoBehaviour {
  5.    
  6.     //Here you create your Array
  7.     public GameObject[] targetButtons;
  8.    
  9.     // Update is called once per frame
  10.     void Update () {
  11.  
  12.  
  13.  
  14.         //Here you check the GameObjects in the array
  15.         //But how do you know which button is the one called "me"?
  16.         //Apparantly it's the one you clicked but where is that declared?
  17.         foreach(GameObject me in targetButtons){
  18.  
  19.             //UIEventListener.Get(targetButton).onClick=ItemClicked;
  20.             //Here it will start the ItemClicked Function
  21.             //With the apparent button which is supposedly "me".
  22.             UIEventListener.Get(me).onClick=ItemClicked;
  23.         }
  24.     }
  25.    
  26.     //But now, where does the function get the gameobject from?
  27.     // Yes I am this big of a noob.
  28.     public void ItemClicked(GameObject btnObj)
  29.     {
  30.    
  31.             Debug.Log(btnObj.name+" was pressed");
  32.     }
  33. }
« Last Edit: February 11, 2013, 12:09:43 PM by 2cat »

redhawk

  • Newbie
  • *
  • Thank You
  • -Given: 1
  • -Receive: 0
  • Posts: 34
    • View Profile
Re: UiButtonMessage help: sending name of gameoject?
« Reply #7 on: February 11, 2013, 12:21:44 PM »
Basically inside the update(), the foreach statement checks all the buttons that you assigned in Unity - I have a cube, that I attached this script to.  I expanded the array in Unity and assigned 3 buttons to each slot.

The UIEventListener (it's part of NGUI) checks each object in the array to see if it was clicked (onClick).  There are also others like, onPress...  If one of the buttons was clicked, it will call the function.  It really doesn't care which button was clicked.  It will call the function for each button that was clicked.  So if you click all 3 buttons, it will call the function for each button separately.

Anything inside the () in the function is something you are "sending" to the function.  So I'm "sending" the object from the UIEventListener.

I just assumed you could follow all this since you were using a switch statement.  I like using loops (for, foreach, do/while) instead of if/switch statements cause they feel clean to me.

Hope that makes sense.

redhawk

  • Newbie
  • *
  • Thank You
  • -Given: 1
  • -Receive: 0
  • Posts: 34
    • View Profile
Re: UiButtonMessage help: sending name of gameoject?
« Reply #8 on: February 11, 2013, 12:28:48 PM »
See picture for what it looks like in Unity

2cat

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 10
    • View Profile
Re: UiButtonMessage help: sending name of gameoject?
« Reply #9 on: February 11, 2013, 01:05:23 PM »
But that means that I have to assign all buttons in the inspector right? That would be a problem for me since I am creating some buttons via a for loop which instantiates several buttons.

I think I can get somewhere with this but I don't know if this is ultimately what I'm looking for if I understand what is going on correctly.

redhawk

  • Newbie
  • *
  • Thank You
  • -Given: 1
  • -Receive: 0
  • Posts: 34
    • View Profile
Re: UiButtonMessage help: sending name of gameoject?
« Reply #10 on: February 11, 2013, 01:10:18 PM »
If you can create your buttons via a for loop, then you can do the same thing to assign the buttons in your array.  Maybe in void Awake. 

2cat

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 10
    • View Profile
Re: UiButtonMessage help: sending name of gameoject?
« Reply #11 on: February 11, 2013, 05:21:31 PM »
Thanks for explaining. Especially your explanation about sending info to a function:

Quote
Anything inside the () in the function is something you are "sending" to the function.  So I'm "sending" the object from the UIEventListener.

That made me think a little and at the gym I thought I had the answer, tested it just now and it works perfectly:

  1.         public void OnButton(GameObject buttonPressed)
  2.         {
  3.                 switch(buttonPressed.name)
  4.                 {
  5.                         case "Left_Button":
  6.                                 Debug.Log ("pressed left button");
  7.                         break;
  8.                         case "Right_Button":
  9.                                 Debug.Log ("pressed Right button");
  10.                         break;
  11.                         case "Select_Button":
  12.                                 Debug.Log ("pressed Select button");
  13.                         break;
  14.                 }
  15.         }

Thanks a ton for your help! Its nothing fancy with arrays(can't wrap my head around them completely just yet) but it works well enough for me right now.

redhawk

  • Newbie
  • *
  • Thank You
  • -Given: 1
  • -Receive: 0
  • Posts: 34
    • View Profile
Re: UiButtonMessage help: sending name of gameoject?
« Reply #12 on: February 11, 2013, 06:00:58 PM »
Glad I could help.

Your setup looks ripe for calling other functions within your Switch.  I like arrays because that way I can keep things generic and just reuse whatever code I spent so long trying to create.