Lua code threw exception 'Exception has been thrown by the target of an invocation.'

Announcements, support questions, and discussion for the Dialogue System.
Post Reply
arrnav96
Posts: 2
Joined: Sat Mar 03, 2018 2:30 am

Lua code threw exception 'Exception has been thrown by the target of an invocation.'

Post by arrnav96 »

I've registered a C# method as lua in a script to instantiate a prefab as a child UI gameobject, through the sequencer. This is how I register it -

Code: Select all

Lua.RegisterFunction("SpEOC1", this, typeof(SpawnDivider).GetMethod("SpawnEOC1"));

public void SpawnEOC1()
    {
        GameObject Div = (GameObject)Instantiate(EOC1, Scroll.transform);
    }
    
In the Dialogue entry tab, I've called SpEOC1() method in "Sequence" and "Scripts" fields for a specific NPC dialogue.

But this is the error I get -

Dialogue System: Lua code 'SpEOC1()' threw exception 'Exception has been thrown by the target of an invocation.'
UnityEngine.Debug:LogError(Object)
PixelCrushers.DialogueSystem.Lua:RunRaw(String, Boolean, Boolean)
PixelCrushers.DialogueSystem.Lua:Run(String, Boolean, Boolean)
PixelCrushers.DialogueSystem.ConversationModel:GetState(DialogueEntry, Boolean, Boolean, Boolean)
PixelCrushers.DialogueSystem.ConversationModel:GetState(DialogueEntry)
PixelCrushers.DialogueSystem.ConversationController:OnSelectedResponse(Object, SelectedResponseEventArgs)
PixelCrushers.DialogueSystem.ConversationView:SelectResponse(SelectedResponseEventArgs)
PixelCrushers.DialogueSystem.ConversationView:OnSelectedResponse(Object, SelectedResponseEventArgs)
PixelCrushers.DialogueSystem.AbstractDialogueUI:OnClick(Object)
UnityEngine.Component:SendMessage(String, Object, SendMessageOptions)
PixelCrushers.DialogueSystem.UnityUIResponseButton:OnClick() (at Assets/Dialogue System/Scripts/Supplemental/UI/Dialogue UI/UnityUIResponseButton.cs:174)
UnityEngine.EventSystems.EventSystem:Update()

Because of this error, the prefab doesn't instantiate at all but the dialogue system continues gameplay as if nothing happened.

There are 3 cloned scenes using the same dialogue manager. I've heard having multiple dialogue managers in different scenes causes problems. But removing the cloned scenes here is not possible due to how the game was designed by the previous programmer.

Strangely, the instantiation was working a while ago before I made a few other changes, so I know there's nothing wrong with the prefab or the code.

Could somebody please enlighten me on what's happening? Specifically, what does this invocation error mean? -

Lua code 'SpEOC1()' threw exception 'Exception has been thrown by the target of an invocation.'
User avatar
Tony Li
Posts: 22059
Joined: Thu Jul 18, 2013 1:27 pm

Re: Lua code threw exception 'Exception has been thrown by the target of an invocation.'

Post by Tony Li »

Hi,

The exception 'Exception has been thrown by the target of an invocation' is a generic message that means something went wrong in your C# method. In all likelihood, the variable "Scroll" is null.
arrnav96
Posts: 2
Joined: Sat Mar 03, 2018 2:30 am

Re: Lua code threw exception 'Exception has been thrown by the target of an invocation.'

Post by arrnav96 »

I checked, Scroll was not null. It was defined and referenced properly.

I've anyway solved that issue by making a new script having different registered names doing the same thing as before. Also the new script is wrapped in namespace PixelCrushers.DialogueSystem, just in case.

But now another registered method throws the same exception. This time the method is for playing a button press audio clip when player selects a response.

I don't know why this error keeps reappearing.
User avatar
Tony Li
Posts: 22059
Joined: Thu Jul 18, 2013 1:27 pm

Re: Lua code threw exception 'Exception has been thrown by the target of an invocation.'

Post by Tony Li »

Hi,

The namespace isn't required.

The error means there is an error in your code, and the error raised an exception. Most of the time, this is a Null Reference Exception, which means your code tried to access a null value.

If you want more information about errors, add "try...catch" to your code:

Code: Select all

public void SpawnEOC1()
{
    try
    {
        GameObject Div = (GameObject)Instantiate(EOC1, Scroll.transform);
    }
    catch (System.Exception e)
    {
        Debug.LogError(e.Message);
    }
}
Post Reply