Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Resolved error CS1503: Argument 1: cannot convert from 'void' to 'UnityEngine.Events.UnityAction'

Discussion in 'Scripting' started by benyji, Oct 11, 2020.

  1. benyji

    benyji

    Joined:
    Apr 13, 2015
    Posts:
    22
    First time coding in C#, Little confused as to why my method is never being called.
    I believe the error line to be the following, after searching online I cannot pinpoint my problem.
    Code (CSharp):
    1.         Rumbler.onClick.AddListener(TaskOnClick(1));
    Code (CSharp):
    1. public class SirenController : MonoBehaviour
    2. {
    3.     public Button RMBLR;
    4.     public Button AIR_HORN;
    5.     public Button MAN;
    6.     public Button WAIL;
    7.     public Button YELP;
    8.     public Button PRTY;
    9.  
    10.     void Start()
    11.     {
    12.         Button Rumbler = RMBLR.GetComponent<Button>();
    13.         Rumbler.onClick.AddListener(TaskOnClick(1));
    14.     }
    15.  
    16.     void TaskOnClick(int number)
    17.     {
    18.         switch (number)
    19.         {
    20.             case 1:
    21.                 break;
    22.             default:
    23.                 break;
    24.         }
    25.     }
    26. }
    Thank you for your help.
     
  2. Terraya

    Terraya

    Joined:
    Mar 8, 2018
    Posts:
    646
    you need to add an arrow function to that :)

    Code (CSharp):
    1. Rumbler.onClick.AddListener(() => TaskOnClick(1));
     
  3. benyji

    benyji

    Joined:
    Apr 13, 2015
    Posts:
    22
    Thank you very much! Enjoy your weekend!
     
    diego_poveda and Terraya like this.