- Home /
Score keeping script
I'm sorry to post another score keeping script question, but I honestly don't know why my script isn't functioning. I am try to get my GUIText score counter to increase by five every time the the game object bullet(Clone) collides with anythings... very simple, and for some reason that i am not aware of, it doesn't not work, meaning that the score counter stays at 0. Thanks in Advance
var score : int = 0;
//when a bullet collides with anything, i want to add five to the score.
function OnCollisionEnter(theCollision : Collision) {
if (theCollision.gameObject.name == "bullet(Clone)"){
score += 5;
}
}
//manages the guitext that displays the score
function Update() {
guiText.text = "Score: "+score;
}
Answer by sneftel · Apr 30, 2011 at 03:57 AM
I seem to recall that there's a space there... that is, the name should be "bullet (Clone)", not "bullet(Clone)". A considerably more robust way to do that, though, would be to set the bullet prefab's tag to something like "bullet", and then check theCollision.gameObject.tag instead. That's guaranteed not to be renamed.
I am sorry to say that this isn't the solution, there isn't a space
Answer by GlennHeckman · Apr 30, 2011 at 05:31 AM
If the spacing in the name wasn't the issue, try making your score variable private so that the properties panel doesn't retain control over the value.
private var score:int = 0;
Or if the score variable must remain public for use in other scripts, you can put the @HideInInspector on the line above your variable declaration to keep it hidden.
The inspector doesn't "retain control" over component properties at runtime. It sets them to their initial values when the level is loaded, and can be used in the editor to view and modify them at runtime, but it doesn't prevent scripts from changing them, and it doesn't revert those changes.
Answer by extrime · Feb 10, 2013 at 07:36 PM
I have that problem to.If you find how to fix it pls tell me.