Javascript score problems whilst referencing scripts.

Hey guys, I’ve recently been told I need to learn JavaScript as I’m in my second year of university studying Games Design and am having a little trouble. Here’s my problem:

When I hit an enemy (chicken) with my bullet all is well and good and the enemy disappears and does what it is supposed to. I then tried to add a scoring system that gives you +1 to an assigned variable as you hit the 3 targets so the final score would be 3 (add 1 each time). This was fine but when I added the score the gameobject deleted with my destroy function and with that went the assigned variable.

Moving on, to counter that problem I called upon the other script from my gui script which I thought would fix the problem. However, the score doesn’t seem to be adding up (it is not deleting this time) and I cannot see the problem, and yes I have dragged the object from the hierarchy into the script variable slot. Here is my code:

This is my GUI code:

var style : GUIStyle;
var collisionchicken : CollisionChicken;
var chickenHit : int = 0;

function OnTriggerEnter(c:Collider){
		if(c.gameObject.tag =="bulletShot")		
	{
		chickenHit++;
          
		}
	}
	
function OnGUI()
{

    GUI.Label( Rect(515, 10, 100, 20), ""+ chickenHit.ToString("f0"), style );

}  

And here is my script that destroys the chickens:

var explosion : Transform;
var Bwuk : AudioClip;

function OnTriggerEnter(c:Collider){
		if(c.gameObject.tag =="bulletShot")
	{
		audio.PlayOneShot(Bwuk);
			yield WaitForSeconds(0.3);
     		Destroy(gameObject);
     var exp = Instantiate(explosion, gameObject.transform.position, Quaternion.identity);
		}
	}

In case I have missed anything, I will recap quickly. I want the chickenHit variable to add 1 each time the object is destroyed in the external script but at the moment it stays at 0 :frowning: I know you guys are extremely knowledgeable and look forward to learning from you :slight_smile:

Thanks, Jimmy

I would combine these scripts unless you have a good reason to keep them apart. OnTriggerEnter is doing the same amount of work, so just combine those.

I see a couple of things… since I too have recently learned JavaScript and have had issues with score.

code should be

//this gives you the ablity to select a GUIText object from the inspector

var Scorecount : GUIText;

//this is so the count can be read as a string

var scorePrint;

//This allows you to select what the trigger is

var collisionchicken : GameObject;

var chickenHit : int = 0;

//These are for when the object is destroyed

var explosion : Transform;

var Bwuk : AudioClip;

function OnTriggerEnter(c:Collider)

{

   if(c.gameObject.tag =="bulletShot") 

{

   chickenHit+1;

//This will destroy the gameobject that has the trigger on it.

   Destroy (collisionchicken);

   }

}

function Update()

{

scorePrint = chickenHit.ToString();

Scorecount.text = String.Format(scorePrint);

}

I know I forgot to add your sound and explosion but you should be able to figure out where those go…