- Home /
Counting Score
Can somebody help me make a scipt, in JavaScript, that allows when a game object collides with another game object, the score adds by 2?
Thanks
Answer by TargonStudios · Nov 13, 2013 at 08:44 PM
Here is an example:
var score = 0;
function OnCollisionEnter (col : Collision){
score+=2;
}
Or if you wanted the score to be a separate script, You would make 2 scripts
Collider Script:
function OnCollisionEnter (col : Collision){
Score.score+=2;
}
Score Script (You would have to name it "Score"):
static var Score = 0;
(Tip, if you wanted to check what the name of the collider is, You would add in):
function OnCollisionEnter (col : Collision){
if(col.gameObject.name == "CollidersName"){
Score.score+=2;
}
}
Hope this helped!
Answer by tab222 · Nov 13, 2013 at 09:25 PM
Great this works, but how can I display the score in a 3D text with a font that I have?
You could make a script and attach it to the text, that contains:
GetComponent(Text$$anonymous$$esh).text = Score.score.ToString();
(Tip: this should have been a comment on my answer)
(Double Tip: if this helped you, maybe you could mark my answer as accepted :D)
Your answer
![](https://koobas.hobune.stream/wayback/20220613121823im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Age of empires Mouse and Camera controls (SOLVED) 2 Answers
Unitron error? 1 Answer
Simple Tile Map 0 Answers
Scene view being grayed out while changing gameobject active 0 Answers
Pro version 3 Answers