Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 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 this 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. Something like this...

  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 this 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 this...

  public void TransformPowerUp(PowerUp ActivePowerUp)


Hope this 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

Hi @Larry-Dietz

There are three buttons and they all use the ButtonClick function. It doesn't seem to work. I want the gameObject to transform based on the case, using enums. I tried adapting what you gave me, but it's not working.

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


AND:

  public enum PowerUps
       {
           ARROW,
           REVERSE,
           THIRD
       }
       public PowerUps ActivePowerUp = PowerUps.ARROW;
 
 
 
 
       void Start()
       {
           Game$$anonymous$$anager.onClick += TransformPowerUp;
   }
 
 
     public void TransformPowerUp(PowerUps ActivePowerUp)
       {
 
           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;
           }

avatar image Larry-Dietz Trabahoo · Dec 09, 2019 at 06:40 PM 0
Share

Try making the following change...

  public void ButtonClick(PowerUps ActivePowerUp)
      {
          //Checks if there are any listeners
          if (onClick != null)
              onClick(ActivePowerUp);
  
      }

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

Delegates/Events across threads 0 Answers

Unity Events notifications 0 Answers

Can a function derived from MonoBehavior subscribe to a static delegate? 1 Answer

How do Delegates and Events work? 1 Answer

Subscribers of Delegates? 2 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