Score function not working...

…this is probably a school-boy error, but im no pro at programming, so… at least most things are working :wink:

Anyway, I have a script here which I would like to add 1 point after the sound is played and the present destroyed. Only problem is, I can’t get the score to work properly xD

Here is my script:

//this is the script for playing a sound when santa collides with a present!

var HoHoHo : AudioClip;
var score = 0;
// santa's collision...
function OnTriggerEnter () 
{
	audio.clip = HoHoHo;
	audio.Play();
	yield WaitForSeconds (2);
	Destroy (gameObject);
	score ++;
}

function OnGUI()
{
	 GUI.Label (Rect (10, 10, 100, 20), "score");
}

I only need the score to increase after the gameObject is destroyed.

Thanks

Mike :slight_smile:

//this is the script for playing a sound when santa collides with a present!

var HoHoHo : AudioClip;
var score = 0;
// santa's collision...
function OnTriggerEnter () 
{
	audio.clip = HoHoHo;
	audio.Play();
	yield WaitForSeconds (2);
	Destroy (gameObject);
	score ++;
}

function OnGUI()
{
	 GUI.Label (Rect (10, 10, 100, 20), score.ToString());
}

I believe your stuff is working properly. You were just printing ‘score’ with the Label instead of what the score is.

I am still new to this, but it looks to me like you never actually feed the score into your label, your working in java script so I don’t know the exact syntax, but something like

GUI.Label (Rect (10, 10, 100, 20), "Score: ",  score);

Basically concatenate the variable of score to the string of “score” you defined as part of the GUI label call. Just so you know you can call the label anything you want and still have the number stored in score show up… like

GUI.Lable(Rest(10,10,100,20),"PinkHippo", score);

whoah!

Thanks guys!

ok, first script re-do (killer1390’s attempt) worked fine, but the score did not change:P

And the second one bought up a error - BCE0077: It is not possible to invoke an expression of type ‘int’.

So, when you hit a trigger, the score doesn’t go up?

I think that when you destroy your gameObject, you also destroy your score as well. You may have to declare your score variable as static in another script. For example,

  1. create an empty game object (Score)
  2. create a score script (ScoreScript.js) and declare
    static var score: int = 0;
  3. attach the ScoreScript.js to the Score game object
  4. in your script, you have to refer the script name and the variable name…
//this is the script for playing a sound when santa collides with a present!

var HoHoHo : AudioClip;
[COLOR="lime"]//var score = 0;[/COLOR]
// santa's collision...
function OnTriggerEnter () 
{
	audio.clip = HoHoHo;
	audio.Play();
	yield WaitForSeconds (2);
	Destroy (gameObject);
	[B]ScoreScript.score ++;[/B]
}

function OnGUI()
{
	 GUI.Label (Rect (10, 10, 100, 20), [B]ScoreScript.score[/B].ToString());
}

I think that you should put the following function in the your Score game object instead…

function OnGUI()
{
   GUI.Label (Rect (10, 10, 100, 20), [B]score[/B].ToString());
}

Mightymao, you did it :slight_smile:

Thanks dude :slight_smile: