UnityEngine.Object cannot be Converted Error on Button Press?

So I’m getting this error:

ArgumentException: Object of type ‘UnityEngine.Object’ cannot be converted to type ‘StoryboardFrame’.
System.RuntimeType.CheckValue (System.Object value, System.Reflection.Binder binder, System.Globalization.CultureInfo culture, System.Reflection.BindingFlags invokeAttr) (at <695d1cc93cca45069c528c15c9fdd749>:0)

And I’m not sure what’s causing it, I think I’m doing everything right, and I would appreciate any and all help!

Here’s the situation:
I am trying to create a function that will delete an UI element, essentially. If the function is called from a UI button (Where the ‘frame’ argument isn’t set), use a specified object elsewhere (in this case the ‘selectedFrame’ variable). But, if the argument is used (by calling the function in another script), use that instead.
I believe it should work properly, but I am getting the error above.

public void DeleteFrame(StoryboardFrame frame = null) // On UI Button where 'frame' is empty
    {
        if (!selectedFrame && frame==null) // Exit the function if there isn't anything to delete
            return;
        // Here's all the code.  every time I need a reference to a 'StoryboardFrame' object,
        // I either use the 'selectedFrame' variable from elsewhere in the script 
        // (when it's called via a button) or the 'frame' argument (called via a script)
        storyboardFrames.Remove(frame?frame: selectedFrame);
        Destroy(frame? frame.activeFrameParent: selectedFrame.activeFrameParent);
        Destroy(frame? frame.gameObject : selectedFrame.gameObject);
        if(!frame) selectedFrame = null;
        plusButton.SetActive(true);
    }

I hope this makes sense, I did my best to make it as clear as possible. I’m only getting the error when it’s called via a UI button press.

Thanks!

Your code is throwing a

Too many ternary operators to reason about... head has exploded

error in my brain.

Steps to success:

  • find the actual spot that is failing
  • google the actual error you are getting
  • reason if what you are attempting to do is possible
  • simplify until you can see the one single step that is failing.

You must find a way to get the information you need in order to reason about what the problem is.

What is often happening in these cases is one of the following:

  • the code you think is executing is not actually executing at all
  • the code is executing far EARLIER or LATER than you think
  • the code is executing far LESS OFTEN than you think
  • the code is executing far MORE OFTEN than you think
  • the code is executing on another GameObject than you think it is

To help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

Doing this should help you answer these types of questions:

  • is this code even running? which parts are running? how often does it run? what order does it run in?
  • what are the values of the variables involved? Are they initialized? Are the values reasonable?
  • are you meeting ALL the requirements to receive callbacks such as triggers / colliders (review the documentation)

Knowing this information will help you reason about the behavior you are seeing.

You can also put in Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene

You could also just display various important quantities in UI Text elements to watch them change as you play the game.

If you are running a mobile device you can also view the console output. Google for how on your particular mobile target.

Here’s an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong:

Hey Kurt, thanks for the reply.
I apologize, I thought it was pretty simple: Depending on if the argument is null, I either use that or a different StoryboardFrame object specified elsewhere in the script.

The error isn’t mentioning any line in any scripts other than Unity’s Button and EventSystem classes, which isn’t very helpful. I just know that pressing that UI button calls this function.
Unless I’m the first person to encounter this error (I’m not), I was hoping for a little more insight as to what could be causing it in this case–is something missing? Am I “converting” the object incorrectly? VS isn’t showing any errors, just the Unity Editor.

In any case, I will mess around with Debug.Log()s some more–I appreciate the suggestion, however generic it may be! :smile: