Hello Guest

Author Topic: tk2duiitem - gameobject sender  (Read 3509 times)

mygamingproject

  • Newbie
  • *
  • Posts: 37
    • View Profile
tk2duiitem - gameobject sender
« on: October 25, 2014, 11:40:19 am »
I have read through the forums and tutorials and struggling to get the sender gameobject on button click.

I have a prefab button that is instantiated.  There is a button controller which is the target of the tk2duiitem with the OnClick event being the following method

Code: [Select]
public void LoadGameClicked()
{
Debug.Log ("Load Game Clicked");
}

Which works fine

However if I want to access the parent GameObject so I can interrogate various parameters, classes etc on what to do next I'm not sure how to interpret the message.

For example,

Code: [Select]
public void LoadGameClicked(GameObject obj)
{
Debug.Log ("Load Game Clicked");
}

Errors with

MissingMethodException: The best match for method LoadGameClicked has some invalid parameter.

System.MonoType.InvokeMember (System.String name, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object target, System.Object[] args, System.Reflection.ParameterModifier[] modifiers, System.Globalization.CultureInfo culture, System.String[] namedParameters) (at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System/MonoType.cs:520)
UnityEngine.SetupCoroutine.InvokeMember (System.Object behaviour, System.String name, System.Object variable)
UnityEngine.GameObject:SendMessage(String, Object, SendMessageOptions)
tk2dUIItem:DoSendMessage(String) (at Assets/TK2DROOT/tk2dUI/Code/Core/tk2dUIItem.cs:246)
tk2dUIItem:Release() (at Assets/TK2DROOT/tk2dUI/Code/Core/tk2dUIItem.cs:266)
tk2dUIManager:CheckInputs() (at Assets/TK2DROOT/tk2dUI/Code/Core/tk2dUIManager.cs:443)
tk2dUIManager:Update() (at Assets/TK2DROOT/tk2dUI/Code/Core/tk2dUIManager.cs:321)

How do I get hold of the gameobject, any assistance would be greatly appreciated?

mygamingproject

  • Newbie
  • *
  • Posts: 37
    • View Profile
Re: tk2duiitem - gameobject sender
« Reply #1 on: October 25, 2014, 09:20:07 pm »
Rightly or wrongly I have gone about this a different way.

I had a buttonbehaviour object and script in scene but this couldn't be applied to the prefab.  So instead I changed tact and added the tk2duiitem via code..

Code: [Select]
private tk2dUIItem uiItem;

void OnEnable()
{
uiItem.OnClick += Clicked;
}

void OnDisable()
{
uiItem.OnClick -= Clicked;
}


void Awake()
{
uiItem = gameObject.AddComponent<tk2dUIItem>();
}

void Clicked()
{
Debug.Log ("Button clicked");


}

This seems to work and now means I have access to the data and object I needed in the first instance
« Last Edit: October 25, 2014, 09:23:57 pm by mygamingproject »

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: tk2duiitem - gameobject sender
« Reply #2 on: October 26, 2014, 09:35:43 pm »
Hi,

I don't think you need to do that either. You can drag the object (itself) into the sendmessage target slot, and simply select the function name in OnClick. Doesn't that work for you?