- Home /
I want the gameobject field of my persistent listeners to reference an object other than the one they are on. Is this possible?
Hello. I can not well explain my problem, so instead here is a picture!
... and here is my code....
[ExecuteInEditMode] public class AssignButtonFunctions : MonoBehaviour
{
public GameObject receiver1;
public void OnClickEvent(GameObject go)
{
Debug.Log("test");
}
void OnEnable()
{
UnityAction<GameObject> action = new UnityAction<GameObject>(this.OnClickEvent);
UnityEventTools.AddObjectPersistentListener<GameObject>(GetComponent<Button>().onClick, action, receiver1);
}
wefwefw8.png
(53.2 kB)
Comment
Best Answer
Answer by Hellium · May 02, 2019 at 02:17 PM
The receiver1
must have a script with a function taking a GameObject
as parameter
[ExecuteInEditMode] public class AssignButtonFunctions : MonoBehaviour
{
public GameObject receiver1;
void OnEnable()
{
UnityAction<GameObject> action = new UnityAction<GameObject>(receiver1.GetComponent<OtherScript>().FunctionToCall );
UnityEventTools.AddObjectPersistentListener<GameObject>(GetComponent<Button>().onClick, action, receiver1);
}
Your answer
Follow this Question
Related Questions
How to access the persistent listener gameobjects on a button? 1 Answer
Check if a listener has already been added to a button? 2 Answers
Any way to pass the function of a listener as a string? 1 Answer
Using listeners to complete code errors 0 Answers
UI Button OnClick Function - How to get name of button that was clicked? 2 Answers