Object of type 'UnityEngine.Object' cannot be converted to type 'UnityEngine.Game

Hi all. I have a pickup script and inside that script, I have a function that is called when a button is pressed. once the button is pressed, it instantiates a weapon. But when i press the button in play mode, I get this error:

ArgumentException: Object of type ‘UnityEngine.Object’ cannot be converted to type ‘UnityEngine.GameObject’.

Pickup script:

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Pickup : MonoBehaviour
{
    #region variables
    public GameObject gunPos;
    public GameObject pickUpUI;
    public GameObject[] Guns;

    public Transform gunParent;

    [SerializeField]private Vector3 pickUpUIOffset;

    [SerializeField]private Shooting playerShooting;
    #endregion

    private void Awake()
    {
        pickUpUI.SetActive(false);
    }

    private void LateUpdate()
    {
        pickUpUI.transform.position = Camera.main.WorldToScreenPoint(transform.position + pickUpUIOffset);
    }

    private void OnTriggerEnter2D(Collider2D collision)
    {
        if (collision.gameObject.CompareTag("PickUp"))
        {
            Destroy(collision.gameObject);
            Selection();
        }
    }

    public void Selection()
    {
        Time.timeScale = 0.5f;
        pickUpUI.SetActive(true);
    }

    public void PickUp(GameObject weapon)
    {
        GameObject pickUp = Instantiate(weapon, gunPos.transform.position, gunPos.transform.rotation) as GameObject;
        pickUp.transform.SetParent(gunParent);

        if (pickUp)
        {
            playerShooting = GameObject.FindGameObjectWithTag("Weapon").GetComponent<Shooting>();

            playerShooting.canShoot = true;
            pickUpUI.SetActive(false);
            Time.timeScale = 1f;
        }
    }
}

Any help? Thanks :slight_smile:

P.S yes, I have looked everywhere before coming to the forums but i still cant find a solution

Is that the entire error message? There is no stack trace and line number info?

For starters the error should also have a line number next to it. What line does it tell you it’s at?

Next, this is an ArgumentException. This means it has to do with something you’ve passed as an argument/parameter to a method/function.

Next its description is telling you that it’s something that expects a GameObject, but you’ve passed in an Object.

Sorry for the lack of info. here is the full error message:

ArgumentException: Object of type ‘UnityEngine.Object’ cannot be converted to type ‘UnityEngine.GameObject’.
System.RuntimeType.CheckValue (System.Object value, System.Reflection.Binder binder, System.Globalization.CultureInfo culture, System.Reflection.BindingFlags invokeAttr) (at <231f6c5a042647adb84a9cc42c982c35>:0)
System.Reflection.RuntimeMethodInfo.ConvertValues (System.Reflection.Binder binder, System.Object[ ] args, System.Reflection.ParameterInfo[ ] pinfo, System.Globalization.CultureInfo culture, System.Reflection.BindingFlags invokeAttr) (at <231f6c5a042647adb84a9cc42c982c35>:0)
System.Reflection.RuntimeConstructorInfo.DoInvoke (System.Object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[ ] parameters, System.Globalization.CultureInfo culture) (at <231f6c5a042647adb84a9cc42c982c35>:0)
System.Reflection.RuntimeConstructorInfo.Invoke (System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[ ] parameters, System.Globalization.CultureInfo culture) (at <231f6c5a042647adb84a9cc42c982c35>:0)
System.Reflection.ConstructorInfo.Invoke (System.Object[ ] parameters) (at <231f6c5a042647adb84a9cc42c982c35>:0)
UnityEngine.Events.PersistentCall.GetObjectCall (UnityEngine.Object target, System.Reflection.MethodInfo method, UnityEngine.Events.ArgumentCache arguments) (at <0dc26211fc6f43128da1d70c2c3a8abc>:0)
UnityEngine.Events.PersistentCall.GetRuntimeCall (UnityEngine.Events.UnityEventBase theEvent) (at <0dc26211fc6f43128da1d70c2c3a8abc>:0)
UnityEngine.Events.PersistentCallGroup.Initialize (UnityEngine.Events.InvokableCallList invokableList, UnityEngine.Events.UnityEventBase unityEventBase) (at <0dc26211fc6f43128da1d70c2c3a8abc>:0)
UnityEngine.Events.UnityEventBase.RebuildPersistentCallsIfNeeded () (at <0dc26211fc6f43128da1d70c2c3a8abc>:0)
UnityEngine.Events.UnityEventBase.PrepareInvoke () (at <0dc26211fc6f43128da1d70c2c3a8abc>:0)
UnityEngine.Events.UnityEvent.Invoke () (at <0dc26211fc6f43128da1d70c2c3a8abc>:0)
UnityEngine.UI.Button.Press () (at Library/PackageCache/com.unity.ugui@1.0.0/Runtime/UI/Core/Button.cs:70)
UnityEngine.UI.Button.OnPointerClick (UnityEngine.EventSystems.PointerEventData eventData) (at Library/PackageCache/com.unity.ugui@1.0.0/Runtime/UI/Core/Button.cs:114)
UnityEngine.EventSystems.ExecuteEvents.Execute (UnityEngine.EventSystems.IPointerClickHandler handler, UnityEngine.EventSystems.BaseEventData eventData) (at Library/PackageCache/com.unity.ugui@1.0.0/Runtime/EventSystem/ExecuteEvents.cs:57)
UnityEngine.EventSystems.ExecuteEvents.Execute[T] (UnityEngine.GameObject target, UnityEngine.EventSystems.BaseEventData eventData, UnityEngine.EventSystems.ExecuteEvents+EventFunction`1[T1] functor) (at Library/PackageCache/com.unity.ugui@1.0.0/Runtime/EventSystem/ExecuteEvents.cs:272)
UnityEngine.EventSystems.EventSystem:Update() (at Library/PackageCache/com.unity.ugui@1.0.0/Runtime/EventSystem/EventSystem.cs:501)

Nothing in that call stack is pointing to the ‘Pickup’ class. So what makes you think it’s in that class that made you post it?

This call stack looks to me like it’s a UnityEvent throwing the exception.

Do you have a UnityEvent wired up that will call this Pickup.PickUp method?

That would explain the text of the exception… your Pickup.PickUp method expects a GameObject. Likely the code that invokes the UnityEvent is passing a UnityEngine.Object, thus the issue.

Is it a ‘Button’ that you have pointing at your Pickup???

Yes, i have a button that when pressed, it calls the “pickup” function. I also have put in my weapon gameobject prefab for the argument that the function is taking in.

Did you change the type of one of your public (or serialized) variables but keep its name the same?

If you do this, you must re-drag the object in. Unity will show it but it will be a reference to the type you had before.

For example:

public Transform Thingy;

Drag a Transform in there

Save scene or prefab using this script

Next, change it to:

public GameObject Thingy;

Now the GameObject may look okay but it won’t be. It will be still pointed at the Transform type. Re-drag a GameObject in.

Question.

Did you have this written as:

public void PickUp(UnityEngine.Object weapon)

At first.

Then wire it up.

Then realize you needed it to be a GameObject and edited it to be:

public void PickUp(GameObject weapon)

???

Try doing this. Go to the Button in question… clear the call from the OnClick event. Then re-add it.

And I mean completely clear it. - the handler completely. Then re add it.

I have done this and nothing happened. The error was still the same. I think it may have something to do with the button

What might be happening is the UnityEvent serialized a ref as type UnityEngine.Object because that’s what it was first. Then you changing it in the code… the serialized data doesn’t realize it’s different, and it’s still trying to call it as if it were a UnityEngine.Object. (what is going on is Unity needs to serialize a representation of the method it has to call… you can’t actually serialize a method. And it stored the arguments incorrectly since the signature changed)

Clearing the button and reconfiguring should make the serialized data update its serialized representation to what it should be.

Basically like what Kurt-Dekker said… but on the Button side.

So yeah… follow what I said in my previous post.

just tried that. still not working. i also cut the code and pasted it back in.

Change this:

public void PickUp(GameObject weapon)

To:

public void PickUp(UnityEngine.Object weapon)

And see if the error changes (you may potentially get an error resulting from the Instantiate not returns a GameObject… but we’ll know from the new error you get, if any).

Just to rule out this method as the problem or not.

Maybe also take a screenshot of the inspector for the Button you have wired up… as is we don’t really have enough information to go on.

ok so i changed the code. error message is still the same. I have attached an image of what is on my button

That is definitely a peculiar one.

I’m fairly certain it’s some sort of serialization/configuration/hub-bub. But I’m not quite sure without putting my hands on it.

I’d try restarting Unity, completely resetting up the button to target… but honestly… I’m all :shrugs: right now.

I’m off to see Dune though… so hopefully someone else can help you out.

Thanks for the help so far :smile:

Do you think it could be a bug? I don’t think it is but nothing is working

ok so i have done some testing. i made a new button with the same on click function. that button worked perfectly. so it has something to do with the actual button GameObject

ok so i fixed it. I had to remake all the buttons. So i don’t know what was going on