Author Topic: UIButton script on clone objects - notify slot is empty after instantiated  (Read 4030 times)

Cheong

  • Newbie
  • *
  • Thank You
  • -Given: 3
  • -Receive: 0
  • Posts: 8
    • View Profile
Hi readers,

I have a problem that when I tried to instantiate NGUI sprite button prefab with scene object with script assigned to its UIbutton script under "notify". The link to the script in "notify" area is gone after being instantiated as a clone.

See the pictures for information.

I also tried to use the script I created to assign the object in scene to the UIButton script of the clones, but there are errors.
Here are the partial scripts attached to the buttons that I've tried:

1.) public GameObject lol;

void Start()
{
   lol = GameObject.Find ("CCTVTableController");
   gameObject.GetComponent<UIButton> ().onClick.Add (lol);
}

2.) gameObject.GetComponent<UIButton>().onClick.Add(GameObject.Find("CCTVTableController"));

Am I doing it wrong? Is there any better way that I can do it? Because I need to instantiate the buttons based on the number of CCTV services available that I can get from server.

Thanks :)

devomage

  • Sr. Member
  • ****
  • Thank You
  • -Given: 7
  • -Receive: 67
  • Posts: 250
    • View Profile
Re: UIButton script on clone objects - notify slot is empty after instantiated
« Reply #1 on: September 01, 2015, 04:24:28 PM »
prefabs lose their assignments because they are 'new' objects when instantiated.

try this:


  1. private UIButton button;
  2.  
  3. void Start()
  4. {
  5.         button = transform.FindChild("background").FindChild("yourbutton").GetComponent<UIButton>();
  6.  
  7.         EventDelegate del = new EventDelegate(this, "OnClick_Button");
  8.         EventDelegate.Set(button.onClick, del);
  9. }
  10.  
  11. void OnClick_Button()
  12. {
  13.         Debug.Log("click");
  14. }
  15.  
  16.  


if needed, find your UIRoot:

  1. void Start()
  2. {
  3.         Transform uiroot = GameObject.FindObjectOfType<UIRoot>().transform;
  4. }
  5.  

Cheong

  • Newbie
  • *
  • Thank You
  • -Given: 3
  • -Receive: 0
  • Posts: 8
    • View Profile
Re: UIButton script on clone objects - notify slot is empty after instantiated
« Reply #2 on: September 01, 2015, 10:41:25 PM »
Thanks devomage,

I'll let you know once I updated my code!  ;)
Much appreciated.

Cheong

  • Newbie
  • *
  • Thank You
  • -Given: 3
  • -Receive: 0
  • Posts: 8
    • View Profile
Re: UIButton script on clone objects - notify slot is empty after instantiated
« Reply #3 on: September 02, 2015, 09:44:14 PM »
Thanks very much, devomage.

The code worked.
Here's the update:

   void Start () {
            button = transform.FindChild("EnableorDisable").GetComponent<UIButton>();
            EventDelegate del = new EventDelegate (this, "OnClick_Button");
            EventDelegate.Set (button.onClick, del);
   }

   void OnClick_Button()
   {
      CCTVController linker = GameObject.Find ("CCTVTableController").GetComponent<CCTVController> ();
      string CCTVnameClicked = gameObject.name;
      if (isEnabled == true) {
                  isEnabled = false;
         Debug.Log("also ran");
            linker.DisableCCTV (CCTVnameClicked);
            } else {
                  isEnabled = true;
            linker.EnableCCTV (CCTVnameClicked);
            }

      }

Once again, Thanks very much!  ;)