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;
}
}
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.
Fair point, thanks for the advice!! Ill see if that is the actual issue somehow
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;
}
}
Your answer
Follow this Question
Related Questions
Whats wrong in my button on click event ??? 1 Answer
Question regarding delegates and events 1 Answer
How to use Button.OnClick.AddListener? 2 Answers
Instance variables and this == null in event handler 1 Answer
Button.onClick.AddListener; How to Pass parameter or get which button was Clicked in Handler Method. 4 Answers