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

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

Discussion in 'Scripting' started by HumphreyGames, Oct 22, 2021.

Thread Status:
Not open for further replies.
  1. HumphreyGames

    HumphreyGames

    Joined:
    May 5, 2020
    Posts:
    40
    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:

    Code (CSharp):
    1. using System;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class Pickup : MonoBehaviour
    7. {
    8.     #region variables
    9.     public GameObject gunPos;
    10.     public GameObject pickUpUI;
    11.     public GameObject[] Guns;
    12.  
    13.     public Transform gunParent;
    14.  
    15.     [SerializeField]private Vector3 pickUpUIOffset;
    16.  
    17.     [SerializeField]private Shooting playerShooting;
    18.     #endregion
    19.  
    20.     private void Awake()
    21.     {
    22.         pickUpUI.SetActive(false);
    23.     }
    24.  
    25.     private void LateUpdate()
    26.     {
    27.         pickUpUI.transform.position = Camera.main.WorldToScreenPoint(transform.position + pickUpUIOffset);
    28.     }
    29.  
    30.     private void OnTriggerEnter2D(Collider2D collision)
    31.     {
    32.         if (collision.gameObject.CompareTag("PickUp"))
    33.         {
    34.             Destroy(collision.gameObject);
    35.             Selection();
    36.         }
    37.     }
    38.  
    39.     public void Selection()
    40.     {
    41.         Time.timeScale = 0.5f;
    42.         pickUpUI.SetActive(true);
    43.     }
    44.  
    45.     public void PickUp(GameObject weapon)
    46.     {
    47.         GameObject pickUp = Instantiate(weapon, gunPos.transform.position, gunPos.transform.rotation) as GameObject;
    48.         pickUp.transform.SetParent(gunParent);
    49.  
    50.         if (pickUp)
    51.         {
    52.             playerShooting = GameObject.FindGameObjectWithTag("Weapon").GetComponent<Shooting>();
    53.  
    54.             playerShooting.canShoot = true;
    55.             pickUpUI.SetActive(false);
    56.             Time.timeScale = 1f;
    57.         }
    58.     }
    59. }
    Any help? Thanks :)

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

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,186
    Is that the entire error message? There is no stack trace and line number info?
     
  3. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,514
    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.
     
  4. HumphreyGames

    HumphreyGames

    Joined:
    May 5, 2020
    Posts:
    40
    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)
     
  5. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,514
    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???
     
  6. HumphreyGames

    HumphreyGames

    Joined:
    May 5, 2020
    Posts:
    40
    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.
     
  7. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,522
    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:

    Code (csharp):
    1. public Transform Thingy;
    Drag a Transform in there

    Save scene or prefab using this script

    Next, change it to:

    Code (csharp):
    1. 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.
     
    lordofduct likes this.
  8. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,514
    Question.

    Did you have this written as:
    Code (csharp):
    1. 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:
    Code (csharp):
    1. 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.
     
    Kurt-Dekker likes this.
  9. HumphreyGames

    HumphreyGames

    Joined:
    May 5, 2020
    Posts:
    40
    I have done this and nothing happened. The error was still the same. I think it may have something to do with the button
     
  10. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,514
    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.
     
  11. HumphreyGames

    HumphreyGames

    Joined:
    May 5, 2020
    Posts:
    40
    just tried that. still not working. i also cut the code and pasted it back in.
     
  12. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,514
    Change this:
    Code (csharp):
    1. public void PickUp(GameObject weapon)
    To:
    Code (csharp):
    1. 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.
     
  13. HumphreyGames

    HumphreyGames

    Joined:
    May 5, 2020
    Posts:
    40
    ok so i changed the code. error message is still the same. I have attached an image of what is on my button
     

    Attached Files:

  14. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,514
    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.
     
  15. HumphreyGames

    HumphreyGames

    Joined:
    May 5, 2020
    Posts:
    40
    Thanks for the help so far :D
     
  16. HumphreyGames

    HumphreyGames

    Joined:
    May 5, 2020
    Posts:
    40
    Do you think it could be a bug? I don’t think it is but nothing is working
     
  17. HumphreyGames

    HumphreyGames

    Joined:
    May 5, 2020
    Posts:
    40
    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
     
  18. HumphreyGames

    HumphreyGames

    Joined:
    May 5, 2020
    Posts:
    40
    ok so i fixed it. I had to remake all the buttons. So i don't know what was going on
     
Thread Status:
Not open for further replies.