Debug throwing exceptions

Announcements, support questions, and discussion for the Dialogue System.
Post Reply
VideoMedicineBear
Posts: 11
Joined: Tue Feb 28, 2023 3:52 pm

Debug throwing exceptions

Post by VideoMedicineBear »

Hi! I'm trying to get some C# script to check the dialogue system for the value of a boolean. When a sentence in a conversation is had, the boolean "InLove" turns from false to true and the character follows the player.
I wrote this in the monobehaviour
public bool InLove = DialogueLua.GetVariable("InLove").isBool;

And then later in the Update part of the script wrote this:
if (Lua.IsTrue("(InLove")) {....}
which leads to the script that makes that sprite follow the player.

But I keep getting isdebugbuild exceptions and the program crashes. The actual compiler errors are:

UnityException: get_isDebugBuild is not allowed to be called from a MonoBehaviour constructor (or instance field initializer), call it in Awake or Start instead. Called from MonoBehaviour 'FollowLove' on game object 'Danni'.
See "Script Serialization" page in the Unity Manual for further details.
PixelCrushers.DialogueSystem.Lua.RunRaw (System.String luaCode, System.Boolean debug, System.Boolean allowExceptions) (at Assets/Plugins/Pixel Crushers/Dialogue System/Scripts/Lua/Lua Wrapper/Lua Interpreter/Lua.cs:250)
PixelCrushers.DialogueSystem.Lua.Run (System.String luaCode, System.Boolean debug, System.Boolean allowExceptions) (at Assets/Plugins/Pixel Crushers/Dialogue System/Scripts/Lua/Lua Wrapper/Lua Interpreter/Lua.cs:129)
PixelCrushers.DialogueSystem.Lua.Run (System.String luaCode, System.Boolean debug) (at Assets/Plugins/Pixel Crushers/Dialogue System/Scripts/Lua/Lua Wrapper/Lua Interpreter/Lua.cs:139)
PixelCrushers.DialogueSystem.DialogueLua.InitializeChatMapperVariables () (at Assets/Plugins/Pixel Crushers/Dialogue System/Scripts/MVC/Model/Logic/DB/DialogueLua.cs:119)
PixelCrushers.DialogueSystem.DialogueLua..cctor () (at Assets/Plugins/Pixel Crushers/Dialogue System/Scripts/MVC/Model/Logic/DB/DialogueLua.cs:88)
Rethrow as TypeInitializationException: The type initializer for 'PixelCrushers.DialogueSystem.DialogueLua' threw an exception.
FollowLove..ctor () (at Assets/Scripts/FollowLove.cs:16)

Do you know what I am doing wrong here? I feel like it's probably something simple about how the C# script is talking to Lua, but I don't know what it is.
User avatar
Tony Li
Posts: 21681
Joined: Thu Jul 18, 2013 1:27 pm

Re: Debug throwing exceptions

Post by Tony Li »

Hi,
VideoMedicineBear wrote: Mon May 29, 2023 7:18 pmUnityException: get_isDebugBuild is not allowed to be called from a MonoBehaviour constructor (or instance field initializer), call it in Awake or Start instead. Called from MonoBehaviour 'FollowLove' on game object 'Danni'.
Can you post your FollowLove script?

If not, then don't use Debug.isDebugBuild in any properties or variables that have initializers.
VideoMedicineBear
Posts: 11
Joined: Tue Feb 28, 2023 3:52 pm

Re: Debug throwing exceptions

Post by VideoMedicineBear »

Hi! Here it is:

using System.Collections;
using System.Collections.Generic;
using PixelCrushers;
using PixelCrushers.DialogueSystem;
using UnityEngine;
using UnityEngine.UI;
using static UnityEngine.GraphicsBuffer;

public class FollowLove : MonoBehaviour
{
[SerializeField]
private GameObject player;
private Rigidbody2D body;
public Vector2 velocity = Vector2.one;
public Vector3 offset = new Vector3(-3, 0, 0);
public bool InLove = DialogueLua.GetVariable("InLove").asBool;

void Awake()
{
body = GetComponent<Rigidbody2D>();
}

void Update()
{
if (Lua.IsTrue("(InLove"))
{
Vector2 toTarget = player.transform.position - transform.position + (offset);
float speed = 1.5f;

transform.Translate(toTarget * speed * Time.deltaTime);

}

}
}
User avatar
Tony Li
Posts: 21681
Joined: Thu Jul 18, 2013 1:27 pm

Re: Debug throwing exceptions

Post by Tony Li »

Thanks. This is the offending line:

Code: Select all

public bool InLove = DialogueLua.GetVariable("InLove").asBool;
Try changing your script to:

Code: Select all

using System.Collections;
using System.Collections.Generic;
using PixelCrushers;
using PixelCrushers.DialogueSystem;
using UnityEngine;
using UnityEngine.UI;
using static UnityEngine.GraphicsBuffer;

public class FollowLove: MonoBehaviour {
  [SerializeField]
  private GameObject player;
  private Rigidbody2D body;
  public Vector2 velocity = Vector2.one;
  public Vector3 offset = new Vector3(-3, 0, 0);
  public bool InLove { get { return DialogueLua.GetVariable("InLove").asBool; } }

  void Awake() {
    body = GetComponent < Rigidbody2D > ();
  }

  void Update() {
    if (InLove) {
      Vector2 toTarget = player.transform.position - transform.position + (offset);
      float speed = 1.5 f;

      transform.Translate(toTarget * speed * Time.deltaTime);

    }
  }
}
VideoMedicineBear
Posts: 11
Joined: Tue Feb 28, 2023 3:52 pm

Re: Debug throwing exceptions

Post by VideoMedicineBear »

Thanks so much! That works perfectly.
Really appreciate this!
User avatar
Tony Li
Posts: 21681
Joined: Thu Jul 18, 2013 1:27 pm

Re: Debug throwing exceptions

Post by Tony Li »

Happy to help!
VideoMedicineBear
Posts: 11
Joined: Tue Feb 28, 2023 3:52 pm

Re: Debug throwing exceptions

Post by VideoMedicineBear »

Oh now my problem is that Unity is not resetting the bool to false when I play it again after stopping. Do you know how to fix that? The bool is in the dialogue system and is set to start out as false initially, but seems to remember itself as true.
User avatar
Tony Li
Posts: 21681
Joined: Thu Jul 18, 2013 1:27 pm

Re: Debug throwing exceptions

Post by Tony Li »

Hi,

Are you using an AutoSaveLoad component to automatically save the game state when exiting play mode and restore the saved game state when re-entering play mode?

If not, then it's probably not something directly related to the Dialogue System.
VideoMedicineBear
Posts: 11
Joined: Tue Feb 28, 2023 3:52 pm

Re: Debug throwing exceptions

Post by VideoMedicineBear »

Oh nevermind! I figured out player prefs was saving it. I fixed it.
Thanks again!
Post Reply