- Home /
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 :( I know you guys are extremely knowledgeable and look forward to learning from you :)
Thanks, Jimmy
Sorry, my apologies. The name of the script that contains the variable chickenHit is: $$anonymous$$enu.js The name of the collision script is: CollisionChicken.js
The gameObject attached is called chicken1
Answer by RobbynB · Sep 26, 2012 at 12:21 AM
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...
A huge help to me! Putting me on the right lines for sure, thank you very much! Just be careful with chickenHit+1, make sure it's either chickenHit = chickenHit +1 or chickenHit++. Probably just a typo but just in case! Thank you very much for the help, I appreciate it :)
Additionally I tried to thumbs up you but apparently I cannot do it on this account, I am unsure if I can.
Answer by DaveA · Sep 25, 2012 at 11:07 PM
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 have taken what you said on board and thanks for the reply. Part of the problem is though, it deletes the script when the object is destroyed thus removing the score variable which therefore leads to no output.