Tasharen Entertainment Forum

Support => NGUI 3 Support => Topic started by: technoir on October 16, 2014, 09:14:36 AM

Title: How to find the Root GameObject a clicked UIButton is on
Post by: technoir on October 16, 2014, 09:14:36 AM
Hi,

I'm trying to find the root GameObject if a child object that has a UIButton component. When the child object is clicked, I would like to record the root GameObject. I am using the following which brings up an error;

  1. public GameObject RootGameObject;
  2.  
  3.     void OnClick()
  4.     {
  5.         UIButton btn = UIButton.current;
  6.  
  7.         RootGameObject = btn.transform.root.gameObject;
  8.  
  9.       //  Debug.Log(UIButton.current.name);
  10.     }
  11.  

The error is:
  1. NullReferenceException: Object reference not set to an instance of an object
  2. KeepTrackOfOpenWindow.OnClick () (at Assets/Khoda/Scripts/Q&A/KeepTrackOfOpenWindow.cs:12)
  3. UnityEngine.GameObject:SendMessage(String, Object, SendMessageOptions)
  4. UICamera:Notify(GameObject, String, Object) (at Assets/NGUI/Scripts/UI/UICamera.cs:1065)
  5. UICamera:ProcessTouch(Boolean, Boolean) (at Assets/NGUI/Scripts/UI/UICamera.cs:1808)
  6. UICamera:ProcessMouse() (at Assets/NGUI/Scripts/UI/UICamera.cs:1402)
  7. UICamera:ProcessTouches() (at Assets/NGUI/Scripts/UI/UICamera.cs:1474)
  8. UICamera:Update() (at Assets/NGUI/Scripts/UI/UICamera.cs:1221)
  9.  
  10.  

Any help wuld be great - Thanks
Title: Re: How to find the Root GameObject a clicked UIButton is on
Post by: PoN on October 16, 2014, 10:03:11 PM
  1. RootGameObject = btn.transform.parent.gameObject;
  2.