How to display variable from another game object C#

I’m working on a game that displays your score and i need some help displaying the score from another script. Script for the lemon (object with the variable):

using UnityEngine;
using System.Collections;

public class VarOnClick : MonoBehaviour {
    int Lemons;
	void OnMouseDown()
	{
		transform.localScale += new Vector3(-0.1F, -0.1F, 0);
		Lemons = MultiplyByTwo(Lemons);
		Debug.Log(Lemons);
		transform.localScale += new Vector3(0.1F, 0.1F, 0);
	}


	int MultiplyByTwo (int number)
	{
		int ret;
		ret = number + 1;
		return ret;
	}

	}

I’m guessing that int Lemons; is the score?

Make it public so other scripts can access it: public int Lemons;

Make a script that displays the score somewhere, for example with a UI.Text. You’ll need to create a UI.Text in the scene.

using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class ShowLemonScore : MonoBehaviour {

    public VarOnClick lemonScript;
    public Text uiText;

	// Update is called once per frame
	void Update () {
        uiText.text = lemonScript.Lemons.ToString();
	}
}

Drag and drop the VarOnClick object and UI.Text into this script’s properties. Score should be visible.


By the way, your MultiplyByTwo function doesn’t actually multiply by two. An accurate name would be appropriate.