How to add a score in unity?
I have looked through other questions about adding a score to a unity game and I don't understand the answers. I have made a simple game, in the game I want each piece of food collected to be worth 1 point. I would like to use c sharp to do this.
Can anyone please help me?
if you don't understand the answers then try going through some of the unity tutorials - they show various ways to do that sort of thing, and much more...
Some things you will want to look at: https://unity3d.com/learn/tutorials/modules/beginner/physics/on-collision-enter
https://unity3d.com/learn/tutorials/modules/beginner/live-training-archive/the-new-ui
Have a class Score with the int score. When you collide with the food (which I assume is what you want), call Destroy(food_gameObject) and add one to the Score.score. Link the Score.score variable to a text in the new Unity UI and update the text to show the score.
You might also want to add the following code to your Score class, called singleton pattern. This makes it "impossible" to instantiate more than one class of your score-class.
public static Score instance;
void Awake(){
if (instance != null) {
Destroy (this);
} else {
instance = this;
}
}
Your answer
Follow this Question
Related Questions
How do I get points when clicking an object? 1 Answer
Destroy GameObject inconsistent with Instantiated children 1 Answer
How to move an object on a terrain that will always stay on top of the terrain? 2 Answers
How can i put down a sprite on collision? 0 Answers
How to show an object when collided with 0 Answers