• 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 /
  • Help Room /
avatar image
Question by NeilH176 · Mar 20, 2022 at 10:07 AM · buttoneventonclickdelegatedelegates

Argument Exception when no arguments? Method Arguments are Incompatible

Hi Guys, I have the following class to represent a real world Day and some logic associated with it.

My question is essentially why does ToggleDayAbroad give the error: Method Arguments Are Incompatible" when I press the button, when there are no arguments to pass. There are no errors when setting the listener, only when the listener is being triggered is there an error.

As far as I can see on other posts I have implemented this correctly, I get the button object, and add the delegate function I want to call. Its on the same gameobject and class too, so there should be no issues finding it.

If anyone know where Im going wrong here, that would be a huge help!!

 using System;
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 using UnityEngine.Events;

 public class Day : MonoBehaviour
 {
     // Variables
     public DateTime dt;
     public bool isAbroad;
     public int daysBeforeToday;
     public Color abroadColour;
 
     public Day(DateTime date, bool abroad)
     {
         dt = date;
         isAbroad = abroad;
     }
 
     public void SetupListener()
     {
         Debug.Log("Start_SetupListener");
         Button b = gameObject.GetComponent<Button>();
         b.onClick.AddListener(ToggleDayAbroad);
         Debug.Log("End_SetupListener");
     }
 
     public void ToggleDayAbroad()
     {
         Debug.Log("ToggleDayAbroad");
         isAbroad = !isAbroad;
         gameObject.GetComponent<Image>().color = isAbroad ? abroadColour : Color.white;
     }
 }
 


Comment

People who like this

0 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 Hellium · Mar 20, 2022 at 10:23 AM 0
Share

I can't tell about your button issue, but one thing, for sure is that you shouldn't have a constructor for your class. MonoBehaviours are not supposed to be instantiated using the new operator. You should either instantiate a prefab or create a new gameObject and add the component by code. Then you set the initial values if you want using a method / setters.

avatar image NeilH176 Hellium · Mar 20, 2022 at 10:57 AM 0
Share

Fair point, thanks for the advice!! Ill see if that is the actual issue somehow

avatar image NeilH176 Hellium · Mar 20, 2022 at 11:13 AM 0
Share

That actually fixed it! Thanks!

Refactored Code:

using System; using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using UnityEngine.Events;

 public class DayObjManager : MonoBehaviour
 {
     // Variables
 
     public Color abroadColour;
 
     public Day day;
 
 
     public void SetupListener()
     {
         Debug.Log("Start_SetupListener");
         Button b = gameObject.GetComponent<Button>();
         b.onClick.AddListener(ToggleDayAbroad);
         Debug.Log("End_SetupListener");
     }
 
     public void ToggleDayAbroad()
     {
         Debug.Log("ToggleDayAbroad");
         day.isAbroad = !day.isAbroad;
         gameObject.GetComponent<Image>().color = day.isAbroad ? abroadColour : Color.white;
     }
 }
 
 public class Day
 {
     public DateTime dt;
     public bool isAbroad;
     public int daysBeforeToday;
 
 
     public Day(DateTime date, bool abroad)
     {
         dt = date;
         isAbroad = abroad;
 
     }
 }
 

0 Replies

· Add your reply
  • Sort: 

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.

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

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

Whats wrong in my button on click event ??? 1 Answer

Question regarding delegates and events 1 Answer

Instance variables and this == null in event handler 1 Answer

How to use Button.OnClick.AddListener? 2 Answers

Create game object from UI button click even 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