- Home /
ArgumentException: Object of type 'UnityEngine.Object' cannot be converted to type 'UnityEngine.GameObject'.
Hi, I am trying to use pointer enter/exit events to check when my cursor leaves or enters the collision box of a GUI button. The effect I am going for is when you hover over the button, the Pointer Enter event triggers a method in a script, passing the GameObject that is the button itself to the method, which will check if the gameobject matches a reference to the same gameobject kept in a dictionary, which is how I keep track of the various GUI buttons. The issue is while this works, the Pointer Exit event which I am trying to set to nothing, which should make it null, does not trigger the part of the method where it checks if it is null, instead it gives me this weird error where it says a game object cannot be converted to a game object?? Here's the code:
[CanBeNull] private GameObject hover = null;
private Vector3 cursorPos;
void Update()
{
if (ReferenceEquals(hover, null) && cursorPos != GUIManager.cursor.transform.position)
{
GUIManager.cursor.transform.position = new Vector2(-2000, -2000);
cursorPos = GUIManager.cursor.transform.position;
}
else
{
foreach (var btn in buttons.Values)
{
if (hover == btn && cursorPos != btn.transform.position)
{
var position = btn.transform.position;
GUIManager.cursor.transform.position = new Vector2(position.x+btn.transform.localScale.x+3, position.y);
cursorPos = new Vector2(position.x, position.y);
}
}
}
}
The GUIManager is just a class I have for managing things like keeping track of references to objects that might otherwise be disabled, like the cursor. Here is the error I'm getting:
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 <2c464dcd3e6d4f6784d86bd1010f8293>: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 <2c464dcd3e6d4f6784d86bd1010f8293>:0)
System.Reflection.RuntimeConstructorInfo.DoInvoke (System.Object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) (at <2c464dcd3e6d4f6784d86bd1010f8293>:0)
System.Reflection.RuntimeConstructorInfo.Invoke (System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) (at <2c464dcd3e6d4f6784d86bd1010f8293>:0)
System.Reflection.ConstructorInfo.Invoke (System.Object[] parameters) (at <2c464dcd3e6d4f6784d86bd1010f8293>:0)
UnityEngine.Events.PersistentCall.GetObjectCall (UnityEngine.Object target, System.Reflection.MethodInfo method, UnityEngine.Events.ArgumentCache arguments) (at /home/bokken/buildslave/unity/build/Runtime/Export/UnityEvent/UnityEvent.cs:504)
UnityEngine.Events.PersistentCall.GetRuntimeCall (UnityEngine.Events.UnityEventBase theEvent) (at /home/bokken/buildslave/unity/build/Runtime/Export/UnityEvent/UnityEvent.cs:471)
UnityEngine.Events.PersistentCallGroup.Initialize (UnityEngine.Events.InvokableCallList invokableList, UnityEngine.Events.UnityEventBase unityEventBase) (at /home/bokken/buildslave/unity/build/Runtime/Export/UnityEvent/UnityEvent.cs:656)
UnityEngine.Events.UnityEventBase.RebuildPersistentCallsIfNeeded () (at /home/bokken/buildslave/unity/build/Runtime/Export/UnityEvent/UnityEvent.cs:837)
UnityEngine.Events.UnityEventBase.PrepareInvoke () (at /home/bokken/buildslave/unity/build/Runtime/Export/UnityEvent/UnityEvent.cs:880)
UnityEngine.Events.UnityEvent`1[T0].Invoke (T0 arg0) (at /home/bokken/buildslave/unity/build/Runtime/Export/UnityEvent/UnityEvent/UnityEvent_1.cs:53)
UnityEngine.EventSystems.EventTrigger.Execute (UnityEngine.EventSystems.EventTriggerType id, UnityEngine.EventSystems.BaseEventData eventData) (at Library/PackageCache/com.unity.ugui@1.0.0/Runtime/EventSystem/EventTrigger.cs:220)
UnityEngine.EventSystems.EventTrigger.OnPointerExit (UnityEngine.EventSystems.PointerEventData eventData) (at Library/PackageCache/com.unity.ugui@1.0.0/Runtime/EventSystem/EventTrigger.cs:237)
UnityEngine.EventSystems.ExecuteEvents.Execute (UnityEngine.EventSystems.IPointerExitHandler handler, UnityEngine.EventSystems.BaseEventData eventData) (at Library/PackageCache/com.unity.ugui@1.0.0/Runtime/EventSystem/ExecuteEvents.cs:36)
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)
I honestly have no clue what to do about this, and would appreciate any help, either a way to fix the error or a better way to do this entire process.
Read the error again (and look at what you've pasted into the question title).
It doesn't say "a game object cannot be converted to a game object", it says "...Object cannot be converted to GameObject".
For more help you're probably going to need to tell us which line of your code is throwing the exception, and possibly show us more code.
But IMO you're looking for something where you're assigning or comparing Objects with GameObjects. For example, if the values in the buttons dictionary were of type Object rather than GameObject, you might see an exception of this sort.