Author Topic: How to find out which gameObject is the one holding a calling UIButton?  (Read 8182 times)

technoir

  • Newbie
  • *
  • Thank You
  • -Given: 3
  • -Receive: 1
  • Posts: 27
    • View Profile
Hi,

I'm trying to set up a type of menu system where an 'Open' button in each window (Panel) will open another window, etc. Each of the windows has an 'Open' and  'Close' button. The Close closes that window and also opens (enables), the previous one again (i.e. the one that called it). I was wondering how I might go about detecting (and getting a handle on) which of the windows (so the GameObject ?) was responsible for opening its 'next' window via its 'Open' button. I have tried something like the following but am getting an error (NullReferenceException). The script containing the  EnableNextWindow() function is called via an Event Delegate on the UIButton's OnClick.

Any help would be great - Thanks!

  1. public GameObject thisCalledMe;
  2.  
  3. public void EnableNextWindow()
  4. {
  5.     thisCalledMe = UIButton.current.gameObject;
  6.  
  7.     Debug.Log(thisCalledMe.gameObject.name); // Gives Error
  8.  
  9.     thisCalledMe.SetActive(true); // etc...
  10. }
  11.  
  12.  
  13.  
« Last Edit: November 04, 2014, 07:13:07 AM by technoir »

technoir

  • Newbie
  • *
  • Thank You
  • -Given: 3
  • -Receive: 1
  • Posts: 27
    • View Profile
Re: How to find out which gameObject is the one holding a calling UIButton?
« Reply #1 on: November 04, 2014, 07:55:56 AM »
Any ideas?

BrightBit

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 1
  • Posts: 21
    • View Profile
Re: How to find out which gameObject is the one holding a calling UIButton?
« Reply #2 on: November 04, 2014, 08:07:25 AM »
  1. Debug.Log(thisCalledMe.gameObject.name);

This line seems wrong! The variable thisCalledMe already is a GameObject, isn't it? So it shouldn't even be possible to call thisCalledMe.gameObject!

technoir

  • Newbie
  • *
  • Thank You
  • -Given: 3
  • -Receive: 1
  • Posts: 27
    • View Profile
Re: How to find out which gameObject is the one holding a calling UIButton?
« Reply #3 on: November 04, 2014, 08:12:08 AM »
Thanks for the reply.

D'oh! OK have removed the 'gameObject' after thisCalledMe. Now getting a 'NullReferenceException: Object reference not set to an instance of an object' for the line above it:

  1. thisCalledMe = UIButton.current.gameObject;

Edit: This is working now, was an issue with the way the objects were structured.
« Last Edit: November 04, 2014, 08:21:25 AM by technoir »

BrightBit

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 1
  • Posts: 21
    • View Profile
Re: How to find out which gameObject is the one holding a calling UIButton?
« Reply #4 on: November 04, 2014, 08:26:44 AM »
Can you post the code for your OnClick method?

technoir

  • Newbie
  • *
  • Thank You
  • -Given: 3
  • -Receive: 1
  • Posts: 27
    • View Profile
Re: How to find out which gameObject is the one holding a calling UIButton?
« Reply #5 on: November 04, 2014, 08:33:50 AM »
There is no custom code for the UIButton OnClick method, I just dragged in the object (the window with the Enable script on it), into the UIButton delegates area, to be opened when the button is clicked.

Btw, Did you see my edit in previous post about this now being solved?

BrightBit

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 1
  • Posts: 21
    • View Profile
Re: How to find out which gameObject is the one holding a calling UIButton?
« Reply #6 on: November 04, 2014, 08:39:25 AM »
I did see your post about your fix. Is your EnableNextWindow method only called from an OnClick event of a button or is there another place where you are calling this method? Can you post your stacktrace, i.e. the complete error message with all function calls.

technoir

  • Newbie
  • *
  • Thank You
  • -Given: 3
  • -Receive: 1
  • Posts: 27
    • View Profile
Re: How to find out which gameObject is the one holding a calling UIButton?
« Reply #7 on: November 04, 2014, 08:45:37 AM »
Yes, only called from the OnClick method of the button, nowhere else.

I don't think I've used the Stacktrace before, would this be the overview box in Profiler showing functions?

BrightBit

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 1
  • Posts: 21
    • View Profile
Re: How to find out which gameObject is the one holding a calling UIButton?
« Reply #8 on: November 04, 2014, 08:52:28 AM »
The stacktrace is part of the exception message under the corresponding entry in your console. I've drawn a red rectangle around the part I'm interested in: