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
P.S yes, I have looked everywhere before coming to the forums but i still cant find a solution
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.
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.
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 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