How do I reference a variable from a different script?

I’m trying to make a game but i am trying to make it save and load data from an xml file.
to do so i need to reference a variable from another script.
I’ve seen some things online that should help me, but i’ve tried and they either gave me an error or just didn’t work.

this is the script where i need the variable:

public class SaveState {
	// put variable here

}

this is the script i need the variable from:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Click : MonoBehaviour {

	public UnityEngine.UI.Text kaasDisplay;
	public double kaasperclick = 1;
	public double kaasKperclick = 0;
	public double kaasMperclick = 0;
	public double kaasBperclick = 0;
	public double kaasTperclick = 0;
	public double kaasqperclick = 0;
	public double kaasQperclick = 0;
	public double kaassperclick = 0;
	public double kaasSperclick = 0;
	public double kaasOperclick = 0;
	public double kaasNperclick = 0;
	public double kaas = 0;       //  <--- the important variable


	void Update() {

		string k = kaas.ToString();

		decimal x = decimal.Parse (k, System.Globalization.NumberStyles.Float);
		

		kaasDisplay.text = "Kaas: " + x.ToString ();


		if (kaasperclick >= 1000) {
			kaasperclick -= 1000;
			kaasKperclick += 1;
		}
		if (kaasKperclick >= 1000) {
			kaasKperclick -= 1000;
			kaasMperclick += 1;
		}
		if (kaasMperclick >= 1000) {
			kaasMperclick -= 1000;
			kaasBperclick += 1;
		}
		if (kaasBperclick >= 1000) {
			kaasBperclick -= 1000;
			kaasTperclick += 1;
		}
		if (kaasTperclick >= 1000) {
			kaasTperclick -= 1000;
			kaasqperclick += 1;
		}
		if (kaasqperclick >= 1000) {
			kaasqperclick -= 1000;
			kaasQperclick += 1;
		}
		if (kaasQperclick >= 1000) {
			kaasQperclick -= 1000;
			kaassperclick += 1;
		}
		if (kaassperclick >= 1000) {
			kaassperclick -= 1000;
			kaasSperclick += 1;
		}
		if (kaasSperclick >= 1000) {
			kaasSperclick -= 1000;
			kaasOperclick += 1;
		}
		if (kaasOperclick >= 1000) {
			kaasOperclick -= 1000;
			kaasNperclick += 1;
		}
	}


	void OnTouchDown() {
		kaas += kaasperclick;
		kaas += kaasKperclick * 1000;
		kaas += kaasMperclick * 1000000;
		kaas += kaasBperclick * 1000000000;
		kaas += kaasTperclick * 1000000000000;
		kaas += kaasqperclick * 1000000000000000;
		kaas += kaasQperclick * 1000000000000000000;
		kaas += kaassperclick * 1000000000000000000 * 1000;
		kaas += kaasSperclick * 1000000000000000000 * 1000000;
		kaas += kaasOperclick * 1000000000000000000 * 1000000000;
		kaas += kaasNperclick * 1000000000000000000 * 1000000000000;
		Debug.Log ("Kaas = " + kaas);
			
	}

}

You can use GetComponent on a gameobject to get any component connected to that gameobject including scripts.

It works something like:

public GameObject otherObject; //assign the obj that has the click script in inspector
private Click ClickScript;

void Start() {
ClickScript = otherObject.GetComponent<Click>();
Debug.Log(ClickScript.kaas);
}

As ShadyProductions said you don’t need your scave class to be a monobehavior to get values. However, as scripts are attached to objects, multiple objects may have a script with different values. Thus to get the values from the script you have two options.

  1. If only one objet uses the script you can track that instance of the script in a static variable:

    public class Click : MonoBehavior {
    public static Click instance;

      void start() {
         Click.instance = this.
     }
    

    }
    and access it via:

    Click.instance.yourVariable;
    There are a number of reasons why doing this is unpreferable.

  2. Obtain a gameobject with the script. If you know the name of the object with the script, then you can use GameObject.Find() to obtain the game object, then use Shady’s answer with the Getcomponent to get the script instance:

    public void save() {
    GameObject obj = GameObject.Find(“MyClickManager”);
    Click click = obj.GetComponent();
    someLocalValue = click.myValue;
    }
    If you have multiple objects with the script, you’ll need to get all instances with that script and do work on them. This way is more of less the standard. The other standard is to have all data managing objects parented to a single object and use that object’s children array for faster access than GameObject.Find