Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This question was closed Mar 07, 2016 at 12:57 PM by meat5000 for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by Magnomous · Oct 20, 2015 at 12:46 PM · c#event triggeringcallback

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

  • Sort: 
avatar image
0
Best Answer

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.

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image mateo4632 · Mar 07, 2016 at 01:28 AM 0
Share

@$$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!

avatar image Magnomous mateo4632 · Mar 07, 2016 at 12:33 PM 0
Share

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

avatar image mateo4632 Magnomous · Mar 13, 2016 at 06:57 PM 0
Share

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!

Show more comments
Show more comments

Follow this Question

Answers Answers and Comments

30 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Passing parameters dynamically with Unity Event 0 Answers

How do you add an UI EventTrigger by Script ? 6 Answers

Custom Event System, Coroutines, Callbacks? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges