- Home /
Question by
BinksyBoy1 · Oct 04, 2014 at 08:55 AM ·
score systemscoreboard
Add point on click of cube?
Hi everyone, I am creating a game and I have become a bit stuck on this bit. Basically, I want it so that when you click on a cube it adds a point. I then want a score box at the top of the screen showing how many points the player has got so far. I'm a bit of a novice to programming so any help would be greatly appreciated!
Comment
Answer by coding_crow · Oct 04, 2014 at 02:15 PM
public class ClickableCube : MonoBehaviour{ // Attach this script to your cube. It also needs a collider attached.
public int timesClicked; // this counts how many times the cube has been clicked
void OnMouseOver() // this is called once per frame on any object that has a collider and is pointed at with the mouse
{
if(Input.GetMouseButtonDown(0)) // if it's not only pointed at, but also clicked
{
timesClicked++; // increase the click counter variable
}
}
}
public class Playerscore{ // this class shows the score on the screen. attach it to any object in the scene.
public ClickableCube cube; // this stores a reference to your cube. Drag the cube into this field in the inspector to assign it.
public Rect scoreRect = new Rect(Screen.Width-200, 0, 200, 50) // this is a rectangle in the top right corner of the screen
void OnGUI()
{
GUI.Label(scoreRect, "Score: " + cube.timesClicked); // this draws the score from the cube into the rectangle
}
}
That said, I think you should work through some basic tutorials.
Your answer
Follow this Question
Related Questions
Making a scoreboard with a timer 2 Answers
Scoring System 3 Answers
How to make a scoreboard? 0 Answers