A little help with a score script (453848)

Hello programming peeps, hope you guys doing well. I been trying to figure this out for a while but I haven’t been able to yet. Well here’s the scenario: I have made a score script which it adds 20 to the score once an enemy is destroyed. It works, but just when I go to the next level, but I want it to add immediately after being destroyed. Here are my scripts:

in cannon.js :

var explosion : Transform;
static var score : int = 0;


function OnTriggerEnter( hit : Collider )
{

	if(hit.gameObject.tag == "playerProjectile")
	{
		Destroy(hit.gameObject);
		var exp = Instantiate(explosion, gameObject.transform.position, Quaternion.identity);
		score += 20;
		Destroy(gameObject);
	}
}

And this is what I got in my GUI.js

//main vars
var clearedPrefab : Transform;
var controlTexture : Texture2D;
var playerScore = MoveAround.score;

function OnGUI () 
{
	// to count the number of enemies
	var coinCount : int = GameObject.FindGameObjectsWithTag("Cannons").Length;
	
	//variable for the score  lives box
	var lifeBox = " Lives Left: "+Health.LIVES+"\n Enemies Left: "+coinCount+"\n Score: "+playerScore;
	
       //score  lives box
	GUI.Box (Rect (Screen.width - 152,10,150,50), lifeBox);
	
}

So this is my code, can you tell me if I am doing something wrong? Like I said, the score does adds the 20 points, but only after I go to the next level(or scene).

cheers =]

This is something I did with my code the the other day:

  1. Make a public variable in your GUI what the score is it’s type (int, string)
  2. Access the public variable by declaring it in cannon.js
    var addScore : GUI; // AddScore is just a name, the word "GUI" means only GUI.cs or GUI.js can be assigned here in the insepector
  3. Add the score to GUI.js, but make sure you declared how to add the score in GUI.js:
addScore.addCannonScore(); // This calls a function addCannonScore() from GUI.js with variable name addScore to add a pre-defined 20

or add 20 to the current variable (int, string).

There would be other ways, but that’s how I did it. If that’s what you’re currently doing by the looks of your current vars in GUI script, then the only other idea I have is that you’re not calling the score from the other script every frame or something.

haha wow now that you mention it, you are definitely right. If I would want to add the score every frame I should have done a script in the Update() function.

I’ll try your script and thanks alot for the reply =]