• 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 May 04, 2021 at 02:15 PM by Trabahoo for the following reason:

Other

avatar image
0
Question by Trabahoo · Dec 09, 2019 at 05:20 PM · switcheventsdelegates

How can I use a delegate/event with enums and switch statement to switch powerup?

So t$$anonymous$$s sits on my powerup and I want to transform it depending on what button is clicked. How can I pass info what button was clicked so the case is switched?

 public enum PowerUps
     {
         ARROW,
         REVERSE,
         THIRD
     }
     public PowerUps ActivePowerUp = PowerUps.ARROW;
 
 
     void Start()
     {
         GameManager.onClick += TransformPowerUp;
 }
 
 
     public void TransformPowerUp()
     {
 
         switch (ActivePowerUp)
         {
             case PowerUps.ARROW:
                 Debug.Log("ARROW");
                 break;
             case PowerUps.REVERSE:
                 Debug.Log("REVERSE");
                 break;
             case PowerUps.THIRD:
                 Debug.Log("THIRD");
                 break;
             default:
                 Debug.Log("NOTHING");
                 break;
         }
 

For the buttons I have the following in my gamemanager script

  public delegate void ActionClick();
     public static event ActionClick onClick;
 
 
     public void ButtonClick()
     {
         //Checks if there are any listeners
         if (onClick != null)
             onClick();
 
     }


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

Answer by Larry-Dietz · Dec 09, 2019 at 05:55 PM

In your GameManager script, where you are defining the onClick event, you should be able to make that event use a parameter, to pass the correct powerup to the script. Somet$$anonymous$$ng like t$$anonymous$$s...

  public delegate void onCliick(PowerUps SelectedPowerUp);
     public static event onClickHandler onClick;
     public static void onClick(PowerUps SelectedPowerUp)
     {
         if (onClick != null)
             onClick(SelectedPowerUp);
     }

I just wrote t$$anonymous$$s directly in the code window, so no guarantees against typos :)

Then in your button event, just pass the correct PowerUp to the OnClick event.

You would also need to change the declaration of TransformPowerUp to accept the parameter, like t$$anonymous$$s...

  public void TransformPowerUp(PowerUp ActivePowerUp)


Hope t$$anonymous$$s helps, -Larry

Comment
Add comment · Show 2 · 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 Trabahoo · Dec 09, 2019 at 06:07 PM 0
Share
avatar image Larry-Dietz Trabahoo · Dec 09, 2019 at 06:40 PM 0
Share

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

119 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 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 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 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

Fire C# Event 3 Answers

NullReferenceException when call an event 1 Answer

How to make event trigger with custom events?? 1 Answer

Chartboost didCompleteRewardedVideo event not working 0 Answers

Can't include System.Threading.Tasks 3 Answers


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