- Home /
The question is answered, right answer was accepted
Assign Event Trigger of object from another object in script
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.
Answer by Magnomous · Oct 20, 2015 at 12:56 PM
Oh... Silly me. I found out that even though I can't see the functions in the editor, they assigned are and work.
@$$anonymous$$agnomous Hey I have a question I am working on a mobile multiplayer game and I have to instantiate the players character when they connect over the network, but the event triggers on the movement buttons set to the prefab. But when it gets instantiated it keeps trying to access the prefab ins$$anonymous$$d of the gameobject that is now in the scene. So my question is how do I get the event triggers to recognize the instantiated object ins$$anonymous$$d of the prefabs. I would think I would need to do it by script but I don't know how. Check the question I posted called EventTrigger on buttons not recognizing instantiated player to see the script. Any help would be great!
I believe that what you need is exactly shown in the script I have posted here. You need to assign in the Start() method Event Trigger event for all of your buttons.
Like this:
EventTrigger.Entry entry1 = new EventTrigger.Entry();
entry1.eventID = EventTriggerType.Drag; // Here would not be drag as you need buttons and not joystick
entry1.callback.AddListener((eventData) => { GetComponent<Player$$anonymous$$ovement>().OnJoystickDrag(); }); // here you call whatever method you want that belongs to script that is attached to this gameobject
joystick.GetComponent<EventTrigger>().triggers[0] = entry1; // assign event that you setup to event trigger
I've being trying to understand this code and I think I get it now, but just one more question, would this need to be attached to the player? Thank you for the help!
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Passing parameters dynamically with Unity Event 0 Answers