Trouble displaying a variable as a GUI from a different script

Hi all,

I’m trying to display the data in the variable “playerScore” from the script “ScoringAndEnemyDestroy”. I want the data from “playerScore” to be placed in the variable “totalScore” in the script “ScoreGUI” so I can display that data as a GUI. The GUI cannot be created in the “ScoringAndEnemyDestroy” script as the GUI repeats itself multiple times as the script is used on multiple colliders.

public class ScoringAndEnemyDestroy : MonoBehaviour {

	public long playerScore;


	// Use this for initialization
	void Start () {

	}
	
	// Update is called once per frame
	public void Update () {
		PlayerScore ();
	}

	public void OnTriggerEnter(Collider other){
		Debug.Log ("Entering collider");

		if (other.gameObject.tag == "Enemy") {
				Destroy (other.gameObject);
				playerScore += 5;

		if  (other.gameObject.tag == "Boss") 
				Destroy (other.gameObject);
				playerScore += 10;	

		
		}
	}

	void PlayerScore()
	{
		SendMessage ("TotalScore", playerScore, SendMessageOptions.DontRequireReceiver);

	}


}

public class ScoreGUI : MonoBehaviour {

	public long totalScore;

	// Use this for initialization
	void Start () {

	}
	
	// Update is called once per frame
	void Update () {

	}

	void TotalScore (long playerScore)
	{
		Debug.Log("PlayerScore: " +playerScore);
		if (playerScore > 0)
						playerScore = totalScore;
		Debug.Log ("Total score is being calculated: " + totalScore);
	}

	void OnGUI() {
		
		GUI.TextArea (new Rect (25,25,50,50), totalScore.ToString());
		
	}
}

All help is greatly appreciated,

Many thanks

SendMessage only broadcasts to the monobehavior scripts in this gameobject(where it was sent from), however BroadcastMessage will do the same, but all children as well. I’m not sure if either of these will suit your needs. You may need to get a reference to the gameobject that ScoreGUI is attached to and use that reference to update the field totalScore. If you can do this instead, you find it’s better in the long run.