Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 post has been wikified, any user with enough reputation can edit it.
avatar image
-1
Question by FirstLaw · Apr 01, 2014 at 08:05 PM · listdelegatepublic variable

Multiple events into one event

Hello , I made many delegates, (clickA,clickB,clickC... etc...) each touch will trigger an action "destroy()" i have 34 prefabs ( each for a letter in the arabic alphabet) i attached to each letter a script like so : using UnityEngine; using System.Collections;

 public class touchA : MonoBehaviour {
 
     void OnEnable()
     {
         Fly fly = GetComponent<Fly>();
         Mainkeyboard.clickA += fly.destroy;
     }
     void OnDisable()
     {
         Fly fly = GetComponent<Fly>();
         Mainkeyboard.clickA -= fly.destroy;
     }
 
 }

So the Question is instead of having to make 34 scripts and then attach each one to a prefab, Am i not able to make one script , that gets attached to prefab and then from a drop down menu(List) and pick which delegate i want?? thank you for your time. here is my main delegate handler.

 using UnityEngine;

 using System.Collections;
 
 public class Mainkeyboard : MonoBehaviour 
 {
     public delegate void ClickAction();
 
     public static event ClickAction clickA;
     public static event ClickAction clickS;
     public static event ClickAction clickD;
     public static event ClickAction clickF;
     public static event ClickAction clickG;
     public static event ClickAction clickH;
     public static event ClickAction clickJ;
     public static event ClickAction clickK;
     public static event ClickAction clickL;
     public static event ClickAction clickL2;
     public static event ClickAction clickL3;
     public static event ClickAction clickQ;
     public static event ClickAction clickW;
     public static event ClickAction clickE;
     public static event ClickAction clickR;
     public static event ClickAction clickT;
     public static event ClickAction clickY;
     public static event ClickAction clickU;
     public static event ClickAction clickI;
     public static event ClickAction clickO;
     public static event ClickAction clickP;
     public static event ClickAction clickP2;
     public static event ClickAction clickP3;
     public static event ClickAction clickQ2;
     public static event ClickAction clickZ;
     public static event ClickAction clickX;
     public static event ClickAction clickC;
     public static event ClickAction clickV;
     public static event ClickAction clickB;
     public static event ClickAction clickN;
     public static event ClickAction clickM;
     public static event ClickAction clickM2;
     public static event ClickAction clickM3;
     public static event ClickAction clickM4;
 }
Comment
Add comment · Show 3
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 FirstLaw · Apr 03, 2014 at 04:02 PM 0
Share

i reedited the question so that it makes more sense.

avatar image whydoidoit · Apr 04, 2014 at 01:16 PM 0
Share

Why don't you have a single delegate that takes a parameter which is the button pressed?

avatar image FirstLaw · Apr 04, 2014 at 02:02 PM 0
Share

how do i do that?

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by fafase · Apr 04, 2014 at 02:31 PM

Considering you have all of your prefabs with the given letter as name then you could simplify by subscribing all of them to one event and the performing a generic check:

 public class Mainkeyboard :MonoBehaviour{
     public delegate void HandlerEvent(string key);
     public static event HandlerEvent OnPress = new HandlerEvent((key)=>{});
 
     void Update(){
         // Check if pressed
         // pass the press key to the event
         if(Input.anyKeyDown)OnPress(Input.inputString);
     }
 }
 
 public class Touch:MonoBehaviour{
    private string givenName;
    void Start()
    {
        givenName = gameObject.name;
        Mainkeyboard.OnPress += CheckKey;
    }
    void CheckKey(string key){
        if(key == givenName)// Do what it should
    } 
    // Remember to remove the subscription on destroy
 }

Now you may have to work that out a little since Input.inputString works with Ascii and I would think arabic letters are unicode.

Also read the doc as it seems to require a little parsing http://docs.unity3d.com/Documentation/ScriptReference/Input-inputString.html

Comment
Add comment · Show 1 · 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 FirstLaw · Apr 04, 2014 at 04:29 PM 0
Share

dude you managed to solve two birds with one stone!!! :D thank you loads!!! , ins$$anonymous$$d of string and given name , i made it into $$anonymous$$eyCode so i can assign each prefab to its own keycode and it works like magic!! thanks again!!!!!!

avatar image
0

Answer by Berenger · Apr 02, 2014 at 07:30 AM

Declare a public integer and use it to access the event array :

 myEvents[eventIndex]();

By "around 30 prefabs", you mean 30 instances of the same prefab right ?

Comment
Add comment · Show 3 · 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 Berenger · Apr 02, 2014 at 07:40 AM 0
Share

An array of event might not be a good idea actually, System.Action seems to be preferable.

avatar image FirstLaw · Apr 02, 2014 at 08:07 AM 0
Share

no i have 30 diffrent prefabs ... (Game objects ) consider them as enemies! ( they are letters ) each letter needs its own keyboard input to kill . on a physical keyboard it managed that easy. but for the touch input i made around 30 buttons for each letter ( they are Arabic letters ) each GUI button gives out a OnClicked delegate event , which then destroys the prefab(the letter) so I made one Delegate handler ( $$anonymous$$ainkeyboard) which has the deffrient buttons and Onclicks I want to make one fly script that allows me to handle all the prefabs at once ... but setting which delegate effects which prefab. as i have it right now . i have to rewrite all of the script 30 times and set each delegate to each prefab ... i think having around 30 scripts for each prefab is a bit crazy!

avatar image FirstLaw · Apr 03, 2014 at 08:35 PM 0
Share

do you have any idea of how i can do it? if it is possible.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

22 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

Related Questions

A node in a childnode? 1 Answer

Sorting a list by distance to an object? 1 Answer

Sequence of methods stored in the list with parameters 1 Answer

Queueing methods and calling them at specific times 1 Answer

"The event can only appear on the left hand side of `+=' or `-=' operator" 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