Search Unity

Cannot Convert from Method Group to Unity Action

Discussion in 'Scripting' started by Spartanias, Oct 8, 2018.

  1. Spartanias

    Spartanias

    Joined:
    Jul 14, 2015
    Posts:
    7
    Hi All,
    so i have from what I can gather, a pretty basic question. I'm pretty new to C# but can understand it through my experience with VBA in MS Excel and MS Access. I have also dabbled in C# with winforms in visual studio.

    My problem now is that C# code i have written beforehand has not transferred well to unity. I literally spent the past 3 days trying to figure out package errors in regard to System.Data and Oracle Data Access client in order to pull information from our Oracle databases at work.

    My issue now is that I an trying to use some c# code as a Login that will check with our LDAP server an authenticate.
    The error, per the thread name is Cannot Convert from Method Group to Unity Action. I'm 99% sure it is how I am trying to pass text box values into variables.

    Code (CSharp):
    1. using UnityEngine.UI;
    2. using UnityEngine;
    3. using Oracle.ManagedDataAccess.Client;
    4. using System.DirectoryServices;
    5. using System;
    6.  
    7. public class NewBehaviourScript : MonoBehaviour
    8. {
    9.  
    10.     //Make sure to attach these Buttons in the Inspector
    11.     public Button m_YourFirstButton;
    12.     public Button m_YourSecondtButton;
    13.  
    14.     void Start()
    15.     {
    16.         Button btn1 = m_YourFirstButton.GetComponent<Button>();
    17.         Button btn2 = m_YourSecondtButton.GetComponent<Button>();
    18.  
    19.         //Calls the TaskOnClick/TaskWithParameters method when you click the Button
    20.         btn1.onClick.AddListener(TaskOnClick);
    21.         btn2.onClick.AddListener(Task2OnClick);
    22.     }
    Code (CSharp):
    1.     void TaskOnClick()
    2.     {
    3.         Debug.Log("Hello1");
    4.         string conString = "Data Source=;User Id=;Password=;";
    5.         OracleConnection con = new OracleConnection();
    6.         con.ConnectionString = conString;
    7.         con.Open();
    8.         OracleCommand cmd = con.CreateCommand();
    9.         cmd.CommandText = "select  from ";
    10.         OracleDataReader reader = cmd.ExecuteReader();
    11.         Debug.Log("Hello2");
    12.         while (reader.Read())
    13.         {
    14.             Debug.Log("Employee Name: " + reader.GetString(0));
    15.         }
    16.        
    17.     }
    Code (CSharp):
    1.     public String _path;
    2.     public String _filterAttribute;
    3.     public Text Username_field;
    4.     public Text InputField;
    5.  
    6.     void Task2OnClick(object sender, EventArgs e)
    7.     {
    8.         bool isAuthenticated;
    9.         String Usrnm = Username_field.text.ToString();
    10.         String Inputfld = InputField.text.ToString();
    11.         String domainAndUsername = "Corp." + @"\" + Usrnm;
    12.  
    13.         try
    14.         {
    15.             using (DirectoryEntry entry = new DirectoryEntry("LDAP.....,DC=....", domainAndUsername, Inputfld))
    16.             {
    17.                 System.Object obj = entry.NativeObject;//changed object to system
    18.                 DirectorySearcher search = new DirectorySearcher(entry);
    19.                 search.Filter = "(SAMAccountName=" + Usrnm + ")";
    20.                 search.PropertiesToLoad.Add("cn");
    21.                 SearchResult result = search.FindOne();
    22.  
    23.                 if (null == result)
    24.                 {
    25.                     isAuthenticated = false;
    26.                 }
    27.  
    28.                 //Update the new path to the user in the directory.
    29.                 _path = result.Path;
    30.                 Debug.Log(_path.ToString());
    31.                 _filterAttribute = (String)result.Properties["cn"][0];
    32.             }
    33.         }
    34.         catch (DirectoryServicesCOMException)
    35.         {
    36.             //wrong Login
    37.             isAuthenticated = false;
    38.             Debug.Log("Invalid Login or Password");
    39.         }
    40.         catch (System.Runtime.InteropServices.COMException)
    41.         {
    42.             // Can't connect
    43.             isAuthenticated = false;
    44.             Debug.Log("Could not connect");
    45.         }
    46.     }
    47.  
    48.     void TaskWithParameters(string message)
    49.     {
    50.         //Output this to console when the Button is clicked
    51.         Debug.Log(message);
    52.     }
    53. }
     
  2. Change this to
    Code (CSharp):
    1. void Task2OnClick()
    I think, that's the problem.

    Also you don't need ToString() when you read the .text since it's a String already.
     
  3. Spartanias

    Spartanias

    Joined:
    Jul 14, 2015
    Posts:
    7
    Perfect, error gone. I still have some issue with my inputbox, unity telling me that its a null reference value, but i will look into that further.
    Thank You.
     
  4. Noir_Nocturne

    Noir_Nocturne

    Joined:
    Oct 25, 2018
    Posts:
    1
    I have the same issue...



    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using Invector.vCharacterController;
    5. #if UNITY_EDITOR
    6. using UnityEditor.Events;
    7. #endif
    8. using Invector;
    9. using Invector.vItemManager;
    10. using System;
    11. namespace EviLA.AddOns.RPGPack
    12. {
    13.     [vClassHeader("Quest Manager", "Quest Manager", iconName = "icon_v2")]
    14.     public class vQuestManager : vMonoBehaviour
    15.     {
    16.         [vEditorToolbar("Mandatory")]
    17.         public vCustomInventory inventoryPrefab;
    18.         public vQuestListData questListData;
    19.         public vItemManager itemManager;
    20.         [vEditorToolbar("Generic")]
    21.         public bool goToNextQuestInListOnFailure = false;
    22.         public bool StartQuestOnAccept = false;
    23.         [vEditorToolbar("Starting Quests")]
    24.         [Header("---Quests Filter---")]
    25.         public List<vQuestType> questsFilter = new List<vQuestType>() { 0 };
    26.  
    27.         #region SerializedProperties in Custom Editor
    28.         [SerializeField]
    29.         public List<QuestReference> startQuests = new List<QuestReference>();
    30.         public List<vQuest> quests;
    31.         [vEditorToolbar("Events")]
    32.         public OnHandleQuestEvent onAddQuest;
    33.         public OnAcceptQuestEvent onAcceptQuest;
    34.         public OnDeclineQuestEvent onDeclineQuest;
    35.         public OnCompleteQuestEvent onCompleteQuestObjective;
    36.         public OnProviderVendorTargetActionEvent onProviderVendorTargetActionEvent;
    37.         public OnOpenCloseInventory onOpenCloseInventory;
    38.         public OnOpenCloseQuestProviderUI onOpenCloseQuestProviderUI;
    39.  
    40.         [HideInInspector]
    41.         public vCustomInventory inventory;
    42.         #endregion
    43.         private Animator animator;
    44.         private static vQuestManager instance;
    45.         public static vQuestManager Instance
    46.         {
    47.             get
    48.             {
    49.                 return instance;
    50.             }
    51.         }
    52.         void OnEnable()
    53.         {
    54.             GameObject gObj = new GameObject("InitialQuestTrigger");
    55.             gObj.hideFlags = HideFlags.HideInHierarchy | HideFlags.HideInInspector;
    56.             gObj.AddComponent<vTriggerInitialQuest>();
    57.             if (instance != null) return;
    58.             instance = this;
    59.             inventory = FindObjectOfType<vCustomInventory>();
    60.             if (!inventory && inventoryPrefab)
    61.             {
    62.                 inventory = Instantiate(inventoryPrefab);
    63.             }
    64.             DontDestroyOnLoad(instance);
    65.             var tpInput = GetComponent<vMeleeCombatInput>();
    66.             if (tpInput) tpInput.onUpdateInput.AddListener(UpdateInput);
    67.         }
    68.         private void UpdateInput(vThirdPersonInput arg0)
    69.         {
    70.             throw new NotImplementedException();
    71.         }
    72.         void Start()
    73.         {
    74.             if (inventory)
    75.             {
    76.                 inventory.GetQuestsHandler = GetQuests;
    77.                 inventory.onAcceptQuest.AddListener(AcceptQuest);
    78.                 inventory.onDeclineQuest.AddListener(DeclineQuest);
    79.                 inventory.onCompleteQuest.AddListener(UpdateQuest);
    80.                 inventory.onProviderVendorTargetActionEvent.AddListener(UpdateQuestFromTarget);
    81.                 inventory.onOpenCloseInventory.AddListener(OnOpenCloseInventory);
    82.                 inventory.onOpenCloseQuestProviderUI.AddListener(OnOpenCloseQuestProviderUI);
    83.             }
    84.             quests = new List<vQuest>();
    85.             if (questListData)
    86.             {
    87.                 for (int i = 0; i < startQuests.Count; i++)
    88.                 {
    89.                     AddQuest(startQuests);
    90.                 }
    91.             }
    92.             animator = GetComponent<Animator>();
    93.         }
    94.         public void LockInventory(bool value)
    95.         {
    96.             itemManager.inventory.lockInput = value;
    97.         }
    98.         public List<vQuest> GetQuests()
    99.         {
    100.             return quests;
    101.         }
    102.  
    103.         public void AddQuest(QuestReference questReference)
    104.         {
    105.             if (questReference != null && questListData != null && questListData.quests.Count > 0)
    106.             {
    107.                 var quest = questListData.quests.Find(t => t.id.Equals(questReference.id));
    108.                 if (quest)
    109.                 {
    110.                     var sameQuests = quests.FindAll(i => i.id == quest.id);
    111.                     if (sameQuests.Count == 0)
    112.                     {
    113.                         var _quest = Instantiate(quest);
    114.                         _quest.name = _quest.name.Replace("(Clone)", string.Empty);
    115.                         if (questReference.attributes != null && _quest.attributes != null && quest.attributes.Count == questReference.attributes.Count)
    116.                             _quest.attributes = new List<vQuestAttribute>(questReference.attributes);
    117.                         quests.Add(_quest);
    118.                         onAddQuest.Invoke(_quest);
    119.                         AddQuest(questReference);
    120.                     }
    121.                 }
    122.             }
    123.         }
    124.         public void RemoveQuests(int i)
    125.         {
    126.             quests.RemoveAll(q => q.id == i);
    127.         }
    128.         public void DeclineQuest(vQuest quest)
    129.         {
    130.             if (quest)
    131.             {
    132.                 inventory.quests.RemoveAll(q => q.id == quest.id);
    133.                 vQuestSystemManager.Instance.UpdateQuestState(quest.id, vQuestState.Failed);
    134.                 bool activeQuestChanged = (quest.id == vQuestSystemManager.Instance.ActiveQuest) ? true : false;
    135.                 vQuestSystemManager.Instance.onActiveQuestChanged.Invoke(quest.id, true, false, activeQuestChanged);
    136.                 UpdateQuest(quest);
    137.                 vQuestSystemManager.Instance.UpdateQuestAccepted(quest.id, false);
    138.                 onDeclineQuest.Invoke(quest);
    139.  
    140.                 if (quest.provider == null)
    141.                     quest.provider = inventory.Provider;
    142.             }
    143.         }
    144.  
    145.         public void AcceptQuest(vQuest quest)
    146.         {
    147.             var questSystem = vQuestSystemManager.Instance;
    148.             AcceptQuest(quest, false, false);
    149.             var forceStartOnAccept = questSystem.IsForceStartOnAccept(quest.id) ? true : questSystem.QuestManager.StartQuestOnAccept;
    150.             vQuestSystemManager.Instance.onActiveQuestChanged.Invoke(quest.id, true, true, forceStartOnAccept);
    151.         }
    152.         public void AcceptQuest(vQuest quest, bool forced = false, bool countdown = false)
    153.         {
    154.             if (quest != null && !vQuestSystemManager.Instance.IsQuestAccepted(quest.id))
    155.             {
    156.                 var state = vQuestSystemManager.Instance.GetQuestState(quest.id);
    157.                 var parent = vQuestSystemManager.Instance.GetParent(quest.id);
    158.                 vQuestSystemManager.Instance.UpdateQuestAccepted(quest.id, true);
    159.                 vQuestSystemManager.Instance.UpdateQuest(quest.id, quest.provider);
    160.                 state = vQuestSystemManager.Instance.GetQuestState(quest.id);
    161.                 if (state == vQuestState.Completed)
    162.                 {
    163.                     if (parent == -1)
    164.                         TriggerActiveQuestChanged(quest.id);
    165.                     else
    166.                         RemoveQuests(quest.id);
    167.                 }
    168.                 else if (state == vQuestState.Failed)
    169.                 {
    170.                     inventory.onDeclineQuest.Invoke(quest);
    171.                     RemoveQuests(quest.id);
    172.                     return;
    173.                 }
    174.                 else
    175.                 {
    176.                     inventory.quests.RemoveAll(q => q.id == quest.id);
    177.                     inventory.quests.Add(quest);
    178.                     if (quest.provider == null)
    179.                         quest.provider = inventory.Provider;
    180.                     vQuestSystemManager.Instance.UpdateQuestState(quest.id, vQuestState.InProgress);
    181.                     AddQuest(new QuestReference(quest.id, quest.provider, quest.QuestTarget));
    182.                     onAcceptQuest.Invoke(quest, false, countdown);
    183.                 }
    184.             }
    185.  
    186.             if (forced && quest != null)
    187.             {
    188.                 TriggerActiveQuestChanged(quest.id, true);
    189.             }
    190.             if (countdown && quest != null)
    191.             {
    192.                 CountDown(quest);
    193.             }
    194.         }
    195.         public void UpdateQuest(vQuest quest)
    196.         {
    197.             var questSystem = vQuestSystemManager.Instance;
    198.             questSystem.UpdateQuest(quest.id, null, null);
    199.             TriggerActiveQuestChanged(quest.id);
    200.         }
    201.         public void UpdateQuestFromActionTrigger(vQuest quest)
    202.         {
    203.             var questSystem = vQuestSystemManager.Instance;
    204.             questSystem.UpdateQuestFromActionTrigger(vQuestTriggerType.Complete, quest.id);
    205.             TriggerActiveQuestChanged(quest.id);
    206.         }
    207.         public void UpdateQuestFromTarget(vQuest quest, vQuestProvider fromProvider = null, IQuestTarget fromTarget = null)
    208.         {
    209.             var questSystem = vQuestSystemManager.Instance;
    210.             vQuestSystemManager.Instance.UpdateQuest(quest.id, fromProvider, fromTarget);
    211.             TriggerActiveQuestChanged(quest.id);
    212.         }
    213.         public void CountDown(vQuest quest)
    214.         {
    215.             if (vQuestSystemManager.Instance.IsQuestAccepted(quest.id))
    216.             {
    217.                 if (vQuestSystemManager.Instance.IsScriptedCountDownEnabled(quest.id))
    218.                     StartCountDown(quest.id);
    219.             }
    220.             else
    221.             {
    222.                 throw new UnityException("Quest needs to be accepted before countdown is started");
    223.             }
    224.         }
    225.         public void TriggerActiveQuestChanged(int id, bool force = false)
    226.         {
    227.             var questSystem = vQuestSystemManager.Instance;
    228.             var state = questSystem.GetQuestState(id);
    229.             var parent = questSystem.GetParent(id);
    230.             //11/01/2017 - Note that the parent will go to NotStarted State from the UpdateQuest set of methods.
    231.             bool changeActiveQuest = parent == -1 ?
    232.                                                 (state == vQuestState.Completed || state == vQuestState.PendingReward || state == vQuestState.Failed) ? true : false
    233.                 : (questSystem.GetQuestState(parent) == vQuestState.Failed || questSystem.GetQuestState(parent) == vQuestState.NotStarted) ? true : false;
    234.             if (force)
    235.                 changeActiveQuest = true;
    236.             //31/08/2017 - If the quest doesn't have a parent, and is not accepted, do not show status.
    237.             //             If the quest has a parent, and the parent is in progress,
    238.             bool showStatus = parent == -1 ? (state == vQuestState.InProgress || questSystem.IsQuestAccepted(id)) ? true : false
    239.                                                 : true;
    240.             questSystem.onActiveQuestChanged.Invoke((parent == -1 ? id : parent), showStatus, false, changeActiveQuest);
    241.         }
    242.         void OnOpenCloseInventory(bool value)
    243.         {
    244.             if (value)
    245.                 animator.SetTrigger("ResetState");
    246.             onOpenCloseInventory.Invoke(value);
    247.         }
    248.         void OnOpenCloseQuestProviderUI(bool value)
    249.         {
    250.             if (value)
    251.                 animator.SetTrigger("ResetState");
    252.             onOpenCloseQuestProviderUI.Invoke(value);
    253.         }
    254.         public void UpdateInput(vMeleeCombatInput tpInput)
    255.         {
    256.             inventory.lockInput = tpInput.lockInventory;
    257.             //tpInput.lockInputByItemManager = inventory.isOpen;
    258.         }
    259.         //Coroutines can only be done via mono-behaviors. As a result the functionality has been moved to QuestManager monobehavior
    260.         public void StartCountDown(int questID)
    261.         {
    262.             StopAllCoroutines();
    263.             StartCoroutine(CountDown(questID));
    264.         }
    265.         protected IEnumerator CountDown(int questID)
    266.         {
    267.             var questSystem = vQuestSystemManager.Instance;
    268.             var duration = questSystem.GetDuration(questID);
    269.             var currentDuration = questSystem.GetElapsedDuration(questID);
    270.             vQuestState state = vQuestState.InProgress;
    271.             while (questSystem.UpdateElapsedDuration(questID, currentDuration))
    272.             {
    273.                 if (Time.timeScale > 0f && (questSystem.ActiveQuest == questID || questSystem.ActiveQuest == questSystem.GetParent(questID)))
    274.                 {
    275.                     state = questSystem.GetQuestState(questID);
    276.                     if (state == vQuestState.Completed || state == vQuestState.PendingReward || state == vQuestState.Failed)
    277.                         break;
    278.                     currentDuration += Time.unscaledDeltaTime;
    279.                 }
    280.                 yield return null;
    281.             }
    282.             state = questSystem.GetQuestState(questID);
    283.             if (state != vQuestState.Completed && state != vQuestState.PendingReward)
    284.             {
    285.                 var quest = questListData.quests.Find(q => q.id == questID);
    286.                 var parent = questListData.quests.Find(q => q.id == questSystem.GetParent(questID));
    287.                 if (state != vQuestState.Failed)
    288.                     DeclineQuest(quest);
    289.             }
    290.             yield return null;
    291.         }
    292.         public void ForceStartQuests()
    293.         {
    294.         }
    295.     }
    296. }
     
    Last edited: Jun 25, 2019
  5. ? What exactly? And please, edit your post and use code tags in your forum post. Your code is not readable. Also please copy and paste the exact error message you got. With the line number and all.
     
  6. hieudinh184

    hieudinh184

    Joined:
    Mar 26, 2019
    Posts:
    3
    I have the exact same problem. It keeps saying can not conver method group to string. I have verry little experience with both unity and coding so i got this code from youtube but for some reason his code works perfectly since mine doesn't
    Here is the code:
    void Attacking()
    {
    anim.SetInteger("Control", 2);
    StartCoroutine (AttackRoutine);
    }
    IEnumerator AttackRoutine()
    {
    yield return new WaitForSeconds (1);
    }
     
  7. Please use code tags if you're posting code on the forum: https://forum.unity.com/threads/using-code-tags-properly.143875/

    Change this
    Code (CSharp):
    1. StartCoroutine (AttackRoutine);
    to this
    Code (CSharp):
    1. StartCoroutine (AttackRoutine());
    And please next time open your own thread. Hijacking other people's thread is not good. Especially when your question has nothing to do with the thread you're posting in.
     
    jlorenzi likes this.
  8. Meta_SJ

    Meta_SJ

    Joined:
    Jun 9, 2022
    Posts:
    1
    I am facing the error Argument 2 cannot convert method group to actions.