How to reference another script

I have a script for variables from my dialogue system. Right now it’s stored in playerprefs and the variables are loaded on starting the game. What I’m trying to do is create a load game button in my main menu and have these variables only recalled if you load a game. (instead of starting a new game).
Apologies if there is a lot of confusion here. I’m very much a beginner and i’m not getting a grasp on this.

This is the part of the script where the variables are loaded in the dialogue variables script:

using System.Collections.Generic;
using UnityEngine;
using Ink.Runtime;
public class DialogueVariables 
{
    public Dictionary<string, Ink.Runtime.Object> variables { get; private set; }
    public Story globalVariablesStory;
    private const string saveVariablesKey = "INK_VARIABLES";
    public DialogueVariables(TextAsset loadGlobalsJSON)
    {
        // create the story
        globalVariablesStory = new Story(loadGlobalsJSON.text);
        Debug.Log("Created the story");
        // if we have saved data, load it
        if (PlayerPrefs.HasKey(saveVariablesKey))
        {
            Debug.Log("There is saved data");
            string jsonState = PlayerPrefs.GetString(saveVariablesKey);
            globalVariablesStory.state.LoadJson(jsonState);
        }
        // initialize the dictionairy
        variables = new Dictionary<string, Ink.Runtime.Object>();
        foreach (string name in globalVariablesStory.variablesState)
        {
            Ink.Runtime.Object value = globalVariablesStory.variablesState.GetVariableWithName(name);
            variables.Add(name, value);
            Debug.Log("Initialized global dialogue variable: " + name + " = " + value);
        }
    }

This is the script on the LOAD button

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class ResumeStoryButton : MonoBehaviour
{
    private const string saveVariablesKey = "INK_VARIABLES";
    public void Loadscene()
       
    {
        SceneManager.LoadScene(PlayerPrefs.GetInt("Level"));
        globalVariablesStory = new Story(loadGlobalsJSON.text);
        Debug.Log("Created the story");
        if (PlayerPrefs.HasKey(saveVariablesKey))
        {
            Debug.Log("There is saved data");
            string jsonState = PlayerPrefs.GetString(saveVariablesKey);
            globalVariablesStory.state.LoadJson(jsonState);
        }
    }
}

I didn’t quite understand what you wanted to do. But, if you need a button press to do one thing, but then other code to do only part of that one thing, you just need to split the code into methods that can be called differently.
So, in this example, I’m just tossing out splitting up the code. Just put it into logical chunks.

In this example, I’ve split the data loading to it’s own method. So, if I were to start a new game, I’d just call LoadScene. If it’s a save game, I’d call LoadData which would first load the data and then Load the scene. (Note, this is an example, you should work out breaking down your methods into logical bits however you feel you need to.)

public class ResumeStoryButton : MonoBehaviour
{
    private const string saveVariablesKey = "INK_VARIABLES";
    public void Loadscene()
      
    {
        SceneManager.LoadScene(PlayerPrefs.GetInt("Level"));
        globalVariablesStory = new Story(loadGlobalsJSON.text);
        Debug.Log("Created the story");
        if (PlayerPrefs.HasKey(saveVariablesKey))
        {
            Debug.Log("There is saved data");
            string jsonState = PlayerPrefs.GetString(saveVariablesKey);
            globalVariablesStory.state.LoadJson(jsonState);
        }
    }
}
public class ResumeStoryButton : MonoBehaviour
{
    private const string saveVariablesKey = "INK_VARIABLES";
    public void Loadscene()
       
    {
        SceneManager.LoadScene(PlayerPrefs.GetInt("Level"));
    }

    public void LoadData()
    {
        globalVariablesStory = new Story(loadGlobalsJSON.text);
        Debug.Log("Created the story");
        if (PlayerPrefs.HasKey(saveVariablesKey))
        {
            Debug.Log("There is saved data");
            string jsonState = PlayerPrefs.GetString(saveVariablesKey);
            globalVariablesStory.state.LoadJson(jsonState);
        }
        LoadScene();
    }
}

Don’t try to do it all at once. Break it all apart into its phases and use LOTS of Debug.Log() statements to prove out what you are doing.

  • gather input from the button (eg, make the click print something)
  • make a load-state-and-play test function (eg, fixed load and go)
  • bring the two together.

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
  • you’re getting an error or warning and you haven’t noticed it in the console window

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.

If your problem would benefit from in-scene or in-game visualization, consider using Debug.DrawRay() or Debug.DrawLine() to visualize things like raycasts or distances.

You can also put in Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene manually, looking for all the parts, where they are, what scripts are on them, etc.

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.

Another useful approach is to temporarily strip out everything besides what is necessary to prove your issue. This can simplify and isolate compounding effects of other items in your scene or prefab.

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:

https://discussions.unity.com/t/839300/3

Thanks for the advice. I think my question was phrased poorly because I was thinking about this problem way too complicated. What I now intend to do is set a boolean to be true when the script is being executed by pressing a button. (which already works)

using UnityEngine.SceneManagement;
public class ResumeStoryButton : MonoBehaviour
{  
public void Loadscene()
    {
        loadIsPressed = true;
        SceneManager.LoadScene(PlayerPrefs.GetInt("Level"));
    }
}

When this script is executed the boolean is set to true. The Boolean should originate in DialogueManager class. But I’m not sure how i should set up this reference… I should also probably move it out of the Awake method into an update…

This is the relevant part of the dialogue manager class

    private void Awake()
    {
            instance = this;
// Here is need to check if the Boolean is true (I think)
            dialogueVariables = new DialogueVariables(loadGlobalsJSON);
            Debug.Log("loaded Variables");
            GameObject[] objs = GameObject.FindGameObjectsWithTag("DialogueManager");
    }

What’s the point of the boolean here?

        loadIsPressed = true;
        SceneManager.LoadScene(PlayerPrefs.GetInt("Level"));

You’re already just DOING the load scene. Aren’t you done?

If this is true:

// Here is need to check if the Boolean is true (I think)

and it is in the next scene, you no longer (generally) have access to that random stray boolean.

But again, why do you care? You know you loaded because now you’re here.

If you really DO need that value, then it needs to live in a longer-lived place, such as a GameManager, which is a very generic term for “something that has broader-game-level responsibility” rather than tactical one-scene-only responsibility.

ULTRA-simple static solution to a GameManager:

OR for a more-complex “lives as a MonoBehaviour” solution…

Simple Singleton (UnitySingleton):

Some super-simple Singleton examples to take and modify:

Simple Unity3D Singleton (no predefined data):

Unity3D Singleton with a Prefab (or a ScriptableObject) used for predefined data:

These are pure-code solutions, do not put anything into any scene, just access it via .Instance!

If it is a GameManager, when the game is over, make a function in that singleton that Destroys itself so the next time you access it you get a fresh one, something like:

public void DestroyThyself()
{
   Destroy(gameObject);
   Instance = null;    // because destroy doesn't happen until end of frame
}

There are also lots of Youtube tutorials on the concepts involved in making a suitable GameManager, which obviously depends a lot on what your game might need.