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 /
avatar image
0
Question by Sendatsu_Yoshimitsu · May 20, 2015 at 10:13 AM · c#event

Having trouble subscribing to events from external class

So I have a class whose main job is to manage game logic like game mode, menu behavior, and to capture player input and relay it to other classes:

 public class GameLogic : MonoBehaviour {
 
     public delegate void OnAttack();
     public static event OnAttack SuccesfulAttack;
 
     void Update(){
         if (Input.GetButtonDown("Attack"))
             SuccesfulAttack();
     }
 
 }
 
 public class Combat : MonoBehaviour {
      void DoDamage(){
      }
 }

I would like to subscribe DoDamage in the Combat class to my SuccessfulAttack event, so every time the logic component determines that an attack has been landed successfully, all related functions take effect.

I've been trying to do it through GameLogic's Awake:

 void Awake(){
 Combat myCombat = gameObject.GetComponent<Combat>();
 SuccesfulAttack += myCombat;
 }

However, I get an error, "Value does not fall within the expected range"- is this the wrong way to do this?

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

2 Replies

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

Answer by abhi_360 · May 20, 2015 at 10:31 AM

change these things

  1. Make DoDamage() public

  2. Change this line SuccesfulAttack += myCombat; to SuccesfulAttack += myCombat.DoDamage;

Comment
Add comment · Show 6 · 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 Sendatsu_Yoshimitsu · May 20, 2015 at 10:42 AM 0
Share

Hmm, I see where the problem was, but I still have an error;

 SuccesfulAttack += myCombat.DoDamage;

results in "Operator '+=' cannot be applied to operands of type GameLogic.OnAttack' and 'void'"

avatar image HTwist · May 20, 2015 at 10:59 AM 0
Share

That is probably because you did OnAttack += ins$$anonymous$$d of SuccesfulAttack +=

avatar image abhi_360 · May 20, 2015 at 11:02 AM 0
Share

$$anonymous$$ake sure your DoDamage() functions return type void and has no arguments and OnAttack also has the same signatures

avatar image Sendatsu_Yoshimitsu · May 20, 2015 at 11:14 AM 0
Share

I'm pretty sure they do, this is weird... here's the complete code, this is in GameLogic:

 void Awake(){
     Combat myCombat = gameObject.GetComponent<Combat>();
     SuccesfulAttack += myCombat.DoDamage();
     }

and this is in Combat:

 public void DoDamage(){
         Debug.Log ("Did damage!");
     }
avatar image abhi_360 · May 20, 2015 at 11:19 AM 1
Share

dude SuccesfulAttack += myCombat.DoDamage; no paranthesis

Show more comments
avatar image
0

Answer by HTwist · May 20, 2015 at 10:23 AM

You are subscribing an object "myCombat" to the event which is not correct. You should be subscribing a method which has the same signature as your delegate.

Comment
Add comment · 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

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

21 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Serialize UnityEvent for use in EditorWindow 0 Answers

How to make a function like OnEnable that fire by itself when specific event occurs? 1 Answer

Relationship between Events and Update() 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