Author Topic: how to set the on click notify target when used add child  (Read 6735 times)

Johanna

  • Newbie
  • *
  • Thank You
  • -Given: 1
  • -Receive: 0
  • Posts: 16
    • View Profile
how to set the on click notify target when used add child
« on: October 17, 2014, 02:55:56 PM »
I create several buttons with NGUITools.AddChild and I wonder how I assign them an on click notify target.

I already discovered this question: http://www.tasharen.com/forum/index.php?topic=9767.0


But I an not sure if it covers my problem because I do not understand how to use the code snippets. Could somebody give me an example?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: how to set the on click notify target when used add child
« Reply #1 on: October 18, 2014, 08:52:56 AM »
You don't understand how to use code snipplets? They're code. Use them in your script. After instantiating the button's game object via AddChild you get a game object to work with (that's what AddChild returns!) -- thatGameObject.GetComponent<UIButton>() on it gives you the button. I think you can figure out the rest, as it's just copy/paste the snipplet.

Johanna

  • Newbie
  • *
  • Thank You
  • -Given: 1
  • -Receive: 0
  • Posts: 16
    • View Profile
Re: how to set the on click notify target when used add child
« Reply #2 on: October 25, 2014, 07:42:00 AM »
I can not figure out how to use them right.

I tried to follow your explanation and created this:

  1. grid = new GameObject[width,height];
  2.  
  3.                 for (int x = 0; x < width; x++)
  4.                 {
  5.                         for(int y = 0; y < height; y++)
  6.                         {
  7.  
  8.                                 GameObject gridTile = NGUITools.AddChild(transform.gameObject, tile);
  9.  
  10.  
  11.                                 gridTile.transform.localPosition = new Vector3(gridTile.transform.position.x + (362*x-1),
  12.                                                                                gridTile.transform.position.y + (362*y-1),
  13.                                                                                gridTile.transform.position.z);
  14.  
  15.                                 UIButton button = new UIButton();
  16.                                 button = gridTile.GetComponent<UIButton>();
  17.  
  18.                                 EventDelegate.Set(button.onClick, Buttons.MakeItBlue);
  19.                                 grid[x,y] = gridTile;
  20.                         }
  21.                 }

But I get error messages:
error CS0120: An object reference is required to access non-static member `Buttons.MakeItBlue()'
error CS1502: The best overloaded method match for `EventDelegate.Set(System.Collections.Generic.List<EventDelegate>, EventDelegate)' has some invalid arguments
error CS1503: Argument `#2' cannot convert `object' expression to type `EventDelegate'

Obviously, I don't use the Snippets right and I can't figure out what could be wrong. For example, I looked the "object reference" error up, and what I found is not different from what I did:

  1. UIButton button = new UIButton();
  2. button = gridTile.GetComponent<UIButton>();

I just don't know where to start debugging it, because I only understand the half of what is going on there and how to use the code.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: how to set the on click notify target when used add child
« Reply #3 on: October 25, 2014, 02:57:21 PM »
You can never do "new <MonoBehaviour>". To add monobehaviours in Unity you need to use AddComponent.

So instead of this:
  1. UIButton button = new UIButton();
  2. button = gridTile.GetComponent<UIButton>();
do this:
  1. UIButton button = gridTile.AddComponent<UIButton>();
If you already have a button component present, change AddComponent to GetComponent.

...please consult Unity's documentation for such basics. This is a beginner Unity question.