How can I keep a score counter of how many points the player has collected on the screen?
I want "Score:" and then the score to increase with each collision/pickup of the object.
More specifically, I want "Score: " to become "Score: 1" after picking up a crystal shard, "Score: 2" after picking another, and etc every time the character picks up a crystal shard.
I'm quite new to scripting, and Javascript would be preferred, however C# is fine too.
It's usually best if you try to write the script yourself and then ask for help, rather than just asking someone to write it for you. That's the best way to learn after all. Anyhow, here's a general way to go about it:
Create a UI Canvas and add a Text object to it.
Write a script attached to the player that checks for collisions with the crystals
If a collision is detected, increment the score variable and then update the UI Text
Here are some helpful references: Using Components Collisions OnCollisionEnter() UI Canvas UI Text UI.Text
Hope this helps.
Answer by Tom01098 · Nov 24, 2015 at 09:05 AM
You could create a canvas with a Text element inside. In your script, make sure you are:
using UnityEditor.UI;
And then you can define
public Text yourTextObject;
Drag and drop your text element into the section in the inspector.
In your code, if you have an int variable called score
private void Update()
{
yourTextObject.text = "Score: " + score;
}
To update the on screen score every frame. Hope this helped!