- Home /
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
Can anyone help? Been stuck on this for an irritating amount of time
Answer by Landern · Aug 07, 2014 at 12:19 PM
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.
The thing is, the Debug.Log in ScoreGUI is displaying the "playerScore" variable just fine in the console. And ScoreGUI and "playerScore" are attached to different objects. I just need a way of displaying that same variable within a GUI on the ScoreGUI script, but obviously the send message is only sending to the TotalScore function and not the whole ScoreGUI script.Are you suggesting to use GetComponent? ScoreGUI is attached to an object called DefencePoint.
Yes to GetComponent once your find the gameobject called "DefencePoint", access the componenet and keep a reference to that(stuff it into a variable of the same type(ScoreGUI) and use that reference for updates and getting the latest score. There really should only be one type keeping score, so i hope ScoreGUI isn't' attached to gameobjects all over the place, that would be horrible.
Your answer
![](https://koobas.hobune.stream/wayback/20220613160044im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Adding 1 point after collison 1 Answer
How to make an object enter a gui screen upon a collision? 1 Answer
Why is OnGUI not working? 1 Answer
Adding score when enemy dies (Errors) 2 Answers