Home>

When the UI Button's Onclick () method is executed, with the OnClickGetSprite () method,
I want to assign the transform of the clicked Button to EmojiA.
"Object of type'UnityEngine.Object' cannot be converted to type'UnityEngine.UI.Button' "
I got the error. I would appreciate it if you could tell me how to solve it.

I received a request to describe the number of lines of the error, but even if I look at the error statement, there is no description.
Probably the part of public void OnClickGetSprite (Button button)? I think. Copy and paste the full text of the error.

Object of type'UnityEngine.Object' cannot be converted to type'UnityEngine.UI.Button<Full text>ArgumentException: Object of type'UnityEngine.Object' cannot be converted to type'UnityEngine.UI.Button'.
System.RuntimeType.CheckValue (System.Object value, System.Reflection.Binder binder, System.Globalization.CultureInfo culture, System.Reflection.BindingFlags invokeAttr) (at<fb001e01371b4adca20013e0ac763896>: 0)
System.Reflection.MonoMethod.ConvertValues ​​(System.Reflection.Binder binder, System.Object [] args, System.Reflection.ParameterInfo [] pinfo, System.Globalization.CultureInfo culture, System.Reflection.BindingFlags invokeAttr) (at<fb001e01371b4adca20013e0ac763896>0): 0)
System.Reflection.MonoCMethod.DoInvoke (System.Object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object [] parameters, System.Globalization.CultureInfo culture) (at<fb001e01371b4adca20013e0ac763896>: 0)
System.Reflection.MonoCMethod.Invoke (System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object [] parameters, System.Globalization.CultureInfo culture) (at<fb001e01371b4adca20013e0ac763896>: 0)
System.Reflection.ConstructorInfo.Invoke (System.Object [] parameters) (at<fb001e01371b4adca20013e0ac763896>: 0)
UnityEngine.Events.PersistentCall.GetObjectCall (UnityEngine.Object target, System.Reflection.MethodInfo method, UnityEngine.Events.ArgumentCache arguments) (at<4cc8ec075538416496e5db5d391208ac>: 0)
UnityEngine.Events.PersistentCall.GetRuntimeCall (UnityEngine.Events.UnityEventBase theEvent) (at<4cc8ec075538416496e5db5d391208ac>: 0)
UnityEngine.Events.PersistentCallGroup.Initialize (UnityEngine.Events.InvokableCallList invokableList, UnityEngine.Events.UnityEventBase unityEventBase) (at<4cc8ec075538416496e5db5d391208ac>: 0)
UnityEngine.Events.UnityEventBase.RebuildPersistentCallsIfNeeded () (at<4cc8ec075538416496e5db5d391208ac>: 0)
UnityEngine.Events.UnityEventBase.PrepareInvoke () (at<4cc8ec075538416496e5db5d391208ac>: 0)
UnityEngine.Events.UnityEvent.Invoke () (at<4cc8ec075538416496e5db5d391208ac>: 0)UnityEngine.UI.Button.Press () (at C: /Program Files/2020.1.0f1/Editor/Data/Resources/PackageManager/BuiltInPackages/com.unity.ugui/Runtime/UI/Core/Button.cs:68)
UnityEngine.UI.Button.OnPointerClick (UnityEngine.EventSystems.PointerEventData eventData) (at C:/Program Files/2020.1.0f1/Editor/Data/Resources/PackageManager/BuiltInPackages/com.unity.ugui/Runtime/UI/Core/Button .cs: ​​110)
UnityEngine.EventSystems.ExecuteEvents.Execute (UnityEngine.EventSystems.IPointerClickHandler handler, UnityEngine.EventSystems.BaseEventData eventData) (at C: /Program Files/2020.1.0f1/Editor/Data/Resources/PackageManager/BuiltInPackages/com.unity.ugui/ Runtime/EventSystem/ExecuteEvents.cs: 50)
UnityEngine.EventSystems.ExecuteEvents.Execute [T] (UnityEngine.GameObject target, UnityEngine.EventSystems.BaseEventData eventData, UnityEngine.EventSystems.ExecuteEvents + EventFunction`1 [T1] functor) (at C:/Program Files/2020.1.0f1/Editor /Data/Resources/PackageManager/BuiltInPackages/com.unity.ugui/Runtime/EventSystem/ExecuteEvents.cs:261)
UnityEngine.EventSystems.EventSystem: Update () (at C: /Program Files/2020.1.0f1/Editor/Data/Resources/PackageManager/BuiltInPackages/com.unity.ugui/Runtime/EventSystem/EventSystem.cs:376)


Button inspector information

Corresponding source code
using UnityEngine;
using UnityEngine.UI;
public class EmojiMove: MonoBehaviour
{
    // Position of the first Sprite clicked
    Transform EmojiA;
// Sprite Position that was clicked for the second time
    Transform Emoji B;
    bool isEmojiA;
    bool isEmojiB = true;
    public void ChangePosition ()
    {
        Vector3 tmpA = EmojiA.transform.position;Vector3 tmpB = EmojiB.transform.position;
        EmojiA.transform.position = tmpB;
        EmojiB.transform.position = tmpA;
    }
    public void OnClickGetSprite (Button button)
    {
        if (isEmojiA == false)
        {
            EmojiA = button.transform;
            isEmojiA = true;
            isEmojiB = false;
            Debug.Log (button);
        }
        else if (isEmojiB == false)
        {
            EmojiB = button.transform;
            isEmojiB = true;
            Debug.Log (button);
        }
    }
}