Author Topic: How to find the Root GameObject a clicked UIButton is on  (Read 1996 times)

technoir

  • Newbie
  • *
  • Thank You
  • -Given: 3
  • -Receive: 1
  • Posts: 27
    • View Profile
How to find the Root GameObject a clicked UIButton is on
« 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

PoN

  • Full Member
  • ***
  • Thank You
  • -Given: 1
  • -Receive: 4
  • Posts: 111
    • View Profile
Re: How to find the Root GameObject a clicked UIButton is on
« Reply #1 on: October 16, 2014, 10:03:11 PM »
  1. RootGameObject = btn.transform.parent.gameObject;
  2.  
Worked on Doc&DogAge Of Fury 3D. Actually working on WarMach.