- Home /
How do I make my score go up upon a collision?
As it stands right now, I have a cube and when it is hit by a ball shot by the player, it changes green and plays a noise. I would like to make a score system that goes up with correct answers and down with incorrect answers. Please help, I'm new to Unity! Thanks
var LevelToLoad : String;
public var CorrectSound : AudioClip;
function OnCollisionEnter (Collision){
gameObject.renderer.material.color = Color.green;
audio.PlayOneShot(CorrectSound);
yield WaitForSeconds (3);
Application.LoadLevel(LevelToLoad);
};
//This code is associated with the collision box. If the ball hits it, it will play the designated sound, wait three seconds then advance to the next level.
Answer by hoy_smallfry · Feb 28, 2013 at 01:43 AM
First you need to create a variable that all scripts can be aware of.
With Javascript, you can accomplish this by marking a variable as both public
and static
. The public
keyword makes it so that other scripts can access the variable, and the static
keyword makes it so that you do not need to attach the script to a GameObject to use this variable.
// in a script called "TestScriptA.js"
public static var score : Number = 0
Now, all another script needs to do to access this variable is to call it by the script name, use the .
to show that the "TestScriptA.js" is the owner of the variable:
// in a different script called "TestScriptB.js"
function Update ()
{
// press 'A' down to add 1 to the score
if(Input.GetKeyDown(KeyCode.A))
{
TestScriptA.score++;
}
// press 'S' down to subtract 1 from the score
if(Input.GetKeyDown(KeyCode.S))
{
TestScriptA.score--;
}
}
Now, you need another function to show the score on the screen, if that's what you want to do. You can either put this in "TestScriptA.js", or in a separate script. But be aware that if you want Unity to call OnGUI()
, the script does have to be attached to a GameObject:
function OnGUI()
{
// define the size of the area that will have the text
var rectangle : Rect = Rect(10, 10, 100, 100);
// draws the text in the rectangle on the screen.
GUI.Label(rectangle, "Score: " + TestScriptA.score);
}
I hope that helps!
Answer by TheDefibulator · Feb 28, 2013 at 04:37 AM
Thanks, that helps a lot! What is the if statement for a collision? and also, how do I edit the size of the text? It is too small for the resolution
1) ins$$anonymous$$d of an posting a new answer, post a comment to an anser, like this.
2) what do you mean by "if statement for a collision"? What are you hoping to do?
3) For changing GUI styles, look at this or even this. $$anonymous$$eep in $$anonymous$$d that Unity Answers has tons of questions that have been asked like yours, so do some searching on here and on Google and I guarantee you you'll find the answer to beginner questions.
Thanks! I figured it out. And, I did some pretty extensive searching and most of the suggestions did not fit what I was looking for so I decided to post.
You're new here, right? well usually, when you find an answer that's right for you, you should select the check mark under the vote up/down icons on the correct answer. Cheers!
Your answer
Follow this Question
Related Questions
How to add the right amount of points when my player destroys a gameobject? 0 Answers
Multiple scores from collisions how do I stop after one collsion 3 Answers
Questions about Is Triggers and player collision. HELP! 2 Answers
Score updating after collision 1 Answer
Why am I getting a negative score? 2 Answers