Scripting with Variables error message

Announcements, support questions, and discussion for the Dialogue System.
Post Reply
SCSnow
Posts: 1
Joined: Sat May 29, 2021 7:59 pm

Scripting with Variables error message

Post by SCSnow »

Hi,

I am trying to get a variable that I had created in the Dialogue Manager for scripting. I've looked at other posts but I am not sure why they have not gotten the same error.

Error Message:
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 'ScoreCounter' on game object 'Text'.
-I am new to coding and it looks like it need to use something else but I am not sure what.

Script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using PixelCrushers;
using PixelCrushers.DialogueSystem;

public class ScoreCounter: MonoBehaviour
{
int gold;

void Update()
{
int gold = DialogueLua.GetVariable("playerGold").asInt;
Debug.Log(gold);

}

}

The eventual goal is to keep track of gold and other resources in the dialogue manager and then have it displayed to the player as UI using Unity UI and script.

Thanks for your time,
User avatar
Tony Li
Posts: 22162
Joined: Thu Jul 18, 2013 1:27 pm

Re: Scripting with Variables error message

Post by Tony Li »

Hi,

Is that the entirety of the script? That script shouldn't cause the error you reported. Is it possible that the source of the error is in another script?

By the way, on this forum you can put the special tags [ code] and [/ code] (without the space before the 'c' in code) around code excerpts. It will look like this:

Code: Select all

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

public class ScoreCounter: MonoBehaviour
{
    int gold;

    void Update()
    {
        int gold = DialogueLua.GetVariable("playerGold").asInt;
        Debug.Log(gold);
    }

}
That script should work fine.

The first "int gold;" probably isn't necessary. The Update() method doesn't use it. The Update() method declares a local variable named "gold" instead. (A local variable only exists for the code that's inside a particular method.)

Ultimately you may want to change the code slightly for efficiency, but the first step is always to get your goal working correctly without error messages, so don't worry too much about efficiency right now.
Post Reply