Sending data to different game objects?

I have a bit of a design problem here and not sure how I should solve this problem in Unity.

My game has a scoreboard in the form of a tk2DSprite, it exists in the game scene and is positioned wherever I want to see it.

The score is meant to be incremented everytime a scriptually generated game object (created through a prefab and the clone function) is destroyed.

My problem is that I’m not sure how the game object should ‘talk’ to the scoreboard, I obviously can’t add it to the prefab script as an object so I’m more or less out of ideas here…

I was thinking if I could figure out a way to query the scene and grab all the objects in it I could find the scoreboard that way, but that’s probably not how I’m supposed to do it.

So any ideas?

Well, I personally like doing it with the C# delegate/events and a Singelton Object for the score.

public sealed class Score {
	private static readonly Score instance = new Score();
	private int score;
	public delegate void ScoreUpdatedEventHandler(int newScore);
	public static event ScoreUpdatedEventHandler ScoreUpdate;    
	
	public Score() {
		score = 0;
	}

	public static Score Instance {
		get { return instance; }
	}
	public static Score GetInstance() {
		return instance;
	}
	
	public int Score {
		get { return score; }
	}
}

Since you don’t need to Find and obtain the script with a singleton, you just need Score.Instance.Score += 10 to increase the score by 10. In order to be notified when the score gets updated you’d need to place a script into your score Sprite, which will listen to it.

    Score.Instance.ScoreUpdate += new delegate(int newScore) {
        // Do whatever is necessary on score update here
    }

This is pretty handy as you can register an unlimited number of events to listen to score updates, i.e. Achievement updates and the checks will only be performed if the score was updated and not every frame.

Of course you can also do this inside of your generated objects Awake() Method

private Score score;
void OnAwake() {
    GameObject o = transform.Find("/ScoreObjectInHierarchyView");
    if(o!=null) {
        score = o.GetComponent<Score>();
    }

    if(score==null)
        Debug.LogError("Couldn't find Score Script");
}

But I really hate this approach, is much of hustle to add it every time and the delegate one is pretty easy and keeps the code clean

edit:
You also don’t have a problem [with the event/delegate] if you rename the script object inside of the hierarchy view, that’s where the above code would fail.