Hi.
I am trying to assign Event Trigger of one game object from script of another game object. More specifically:
I’ve got a player that has a component PlayerGUI. This script has got variable RawImage joystick. Joystick is a child object of Canvas, that has Event Trigger. This Event Trigger consists of 2 triggers. First once is about what to do when joystick is being dragged. Second one is about what should happen when dragging ends.
These 2 are already created, but they must be assigned at runtime since player will spawn once the game starts - not before. Both functions “OnJoystickDrag” and “OnJoystickDragExit” are in PlayerMovement script.
This is the PlayerGUI script:
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using System.Collections;
public class PlayerGUI : MonoBehaviour {
public RawImage joystick;
public RawImage joystickBackground;
void Start () {
if(!joystick)
joystick = GameObject.Find("Joystick").GetComponent<RawImage>();
if(!joystickBackground)
joystickBackground = GameObject.Find("JoystickBackground").GetComponent<RawImage>();
EventTrigger.Entry entry1 = new EventTrigger.Entry();
entry1.eventID = EventTriggerType.Drag;
entry1.callback.AddListener((eventData) => { GetComponent<PlayerMovement>().OnJoystickDrag(); });
joystick.GetComponent<EventTrigger>().triggers[0] = entry1;
EventTrigger.Entry entry2 = new EventTrigger.Entry();
entry2.eventID = EventTriggerType.EndDrag;
entry2.callback.AddListener((eventData) => { GetComponent<PlayerMovement>().OnJoystickDragExit(); });
joystick.GetComponent<EventTrigger>().triggers[1] = entry2;
}
}
When I run the game, Joystick’s Event Trigger got 2 triggers, that are correctly set up to Drag and End Drag. But they don’t have any BaseEventData. eventID is correct, but callback must be wrong.
I will be grateful for any help. Thanks in advance.