Page 1 of 1

Debug throwing exceptions

Posted: Mon May 29, 2023 7:18 pm
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.

Re: Debug throwing exceptions

Posted: Mon May 29, 2023 9:50 pm
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.

Re: Debug throwing exceptions

Posted: Tue May 30, 2023 4:22 pm
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);

}

}
}

Re: Debug throwing exceptions

Posted: Tue May 30, 2023 6:19 pm
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);

    }
  }
}

Re: Debug throwing exceptions

Posted: Tue May 30, 2023 6:48 pm
by VideoMedicineBear
Thanks so much! That works perfectly.
Really appreciate this!

Re: Debug throwing exceptions

Posted: Tue May 30, 2023 6:50 pm
by Tony Li
Happy to help!

Re: Debug throwing exceptions

Posted: Tue May 30, 2023 7:10 pm
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.

Re: Debug throwing exceptions

Posted: Tue May 30, 2023 7:38 pm
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.

Re: Debug throwing exceptions

Posted: Tue May 30, 2023 7:42 pm
by VideoMedicineBear
Oh nevermind! I figured out player prefs was saving it. I fixed it.
Thanks again!