Anyone mind to share how to do a score board?
When an object kills something, it put a score to the board.
There is only one scoreboard, and lots of objects want to interact with it, so it makes sense to have a single GameController object that handles tasks like keeping score and ending the game. I believe the racing tutorial has an example of such an object.
You can either have each object send a message which the controller is listening for:
http://unity3d.com/Documentation/ScriptReference/GameObject.SendMessage.html
Or each object can directly call a GameController method directly. This requires that the object “find” the GameController in the scene. The wiki’s AManagerClass script is perfect for this:
http://www.unifycommunity.com/wiki/index.php?title=AManagerClass
Hope this gives you some ideas. Once the GameController has been told that the score has increased, it simply needs to increment a variable that it has been keeping the score in, then print that score into a GUIText on the screen.
I will post some code for my example of scoring when I get home tonight.
looking forward to it davey and thanks bampf for the ref.
Place this script onto a guiText that you want to keep your score:
Score Script:
var score = 0;
function Start () {
guiText.text = score.ToString();
}
function AddToScore () {
score++;
guiText.text = score.ToString();
}
Put this script onto the object that gets destroyed.
// Place the Score guiText onto the score variable in this script
var score : GameObject;
function Kill () { // or whatever
score.GetComponent("Score").AddToScore();
Destroy (gameObject);
}
hmm followed ur steps, but it seem not working for me :<…do u mind making a unitypackage as sample?
Sure. Take a look at this:
Thanks Daniel, got tied up with the kids last night.
function Update () {
if (transform.position.y < 0)
Kill();
}
var score : GameObject;
function Kill () {
score.GetComponent("Score").AddToScore();
Destroy (gameObject);
}
Hi daniel, as i understand that the code
function Update () {
if (transform.position.y < 0)
Kill();
}
is to detect when the enemy dies right ? but for my scene here, the monster is getting killed by the sentry gun. So how am i suppose to write in?
It would probably be easiest if you could post your monster script.
var target : Transform;
var duration = 1.0;
function Start () {
if (!target) {
target = GameObject.FindWithTag("Player").transform;
MoveToPlayer();
}
}
function MoveToPlayer()
{
startPos = transform.position;
endPos = target.position;
for (t=0.0; t < duration; t+= Time.deltaTime)
{
transform.position = Vector3.Lerp(startPos, endPos, t/duration);
yield;
}
}
This is the monsterscript.js , but do this script helps? :?
Does the monster have some hit points or something? If it does post that script.
var hitPoints = 100.0;
var dieSound : AudioClip;
function ApplyDamage (damage : float)
{
// We already have less than 0 hitpoints, maybe we got killed already?
if (hitPoints <= 0.0)
return;
hitPoints -= damage;
if (hitPoints <= 0.0)
{
Detonate();
}
}
function Detonate ()
{
// Destroy ourselves
Destroy(gameObject);
// Play a dying audio clip
if (dieSound)
AudioSource.PlayClipAtPoint(dieSound, transform.position);
}
static function CopyTransformsRecurse (src : Transform, dst : Transform)
{
dst.position = src.position;
dst.rotation = src.rotation;
for (var child : Transform in dst)
{
// Match the transform with the same name
var curSrc = src.Find(child.name);
if (curSrc)
CopyTransformsRecurse(curSrc, child);
}
}
this, CharacterDamage.js
Great, that’s just the script.
Just put it under the function Detonate.
// Place the score GUI into this variable
var score : GameObject;
function Detonate ()
{
// Destroy ourselves
Destroy(gameObject);
// Play a dying audio clip
if (dieSound)
AudioSource.PlayClipAtPoint(dieSound, transform.position);
score.GetComponent("Score").AddToScore();
Destroy (gameObject);
}
thanks alot Daniel its working now!!