- Home /
How to put score and what codes to use
Hi im a beginner in unity and would love to have some help, I cannot see any tutorial regarding my game and i have no idea how to do this script and this is the last thing that i need. I only need the script for 1 scene and i have finished the other and i need this for my thesis.
Ok so here it is, I need an image that is moving from left to right using
using UnityEngine;
using System.Collections;
public class BackAndForth : MonoBehaviour {
public float delta = 100.0f; // Amount to move left and right from the start point
public float speed = 2.0f;
private Vector3 startPos;
void Start () {
startPos = transform.position;
}
void Update () {
Vector3 v = startPos;
v.x += delta * Mathf.Sin (Time.time * speed);
transform.position = v;
}
}
My game is like a dart game that you need to click or shoot the target, the farther away the less score. But the difference is i'm using a princess as the target and the dart will be a kiss. Clicking will trigger the kiss and the target should be the lips. And also i don't know how to put score in that but i have a scoring system from the other scene. Here it is: I got this from one of the tutorials from internet
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class ScoreManager1 : MonoBehaviour {
public Text scoreText;
public Text hiScoreText;
public float scoreCount;
public float hiScoreCount;
public float pointsPerSecond;
public bool scoreIncreasing;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (scoreIncreasing) {
scoreCount -= pointsPerSecond * Time.deltaTime;
}
if (scoreCount > hiScoreCount) {
hiScoreCount = scoreCount;
}
scoreText.text = "Score: " + Mathf.Round (scoreCount);
hiScoreText.text = "High Score: " + Mathf.Round (hiScoreCount);
}
}
I hope you guys can help me with this. Would really appreciate your help i've been stuck here for 1 month :(
https://unity3d.com/learn/tutorials
You don't really have anything going on here yet. The first script is only moving the transform to the right. The second script is subtracting from scoreCount every frame and updating the scoreText and hiScoreText. Have you assigned these to something in the editor? Your whole kiss mechanism isn't shown anywhere here so I have to ignore that. For a high score it sounds more like you want to be calculating the distance than the time. Have you gone through ALL of these?
https://unity3d.com/learn/tutorials
You should go through ALL of them several times until you know what's going on if you have any interest in making games. Doesn't take a month.
Ok i figured it out a little. But my problem now is the High Score system. It restarts everytime i finished the game or the game ends the High Score is not saving i already tried setfloat and get float but i dont know what im missing here are the scripts. using UnityEngine; using System.Collections; using UnityEngine.UI;
public class Score$$anonymous$$anager1 : $$anonymous$$onoBehaviour {
public Text scoreText;
public Text hiScoreText;
public GameObject plus$$anonymous$$enuHolder;
public GameObject $$anonymous$$$$anonymous$$enuHolder;
public float scoreCount;
public float hiScoreCount;
public float pointsPerSecond;
public bool scoreIncreasing;
// Use this for initialization
void Start () {
if (PlayerPrefs.Has$$anonymous$$ey ("HighScore")) {
hiScoreCount = PlayerPrefs.GetFloat ("HighScore");
}
}
// Update is called once per frame
void Update () {
if (scoreIncreasing) {
scoreCount -= pointsPerSecond * Time.deltaTime;
PlayerPrefs.SetFloat("Score", scoreCount);
}
if (scoreCount > hiScoreCount)
{
hiScoreCount = scoreCount;
PlayerPrefs.SetFloat ("HighScore", hiScoreCount);
}
scoreText.text = "Score: " + $$anonymous$$athf.Round (scoreCount);
hiScoreText.text = "High Score: " + $$anonymous$$athf.Round (hiScoreCount);
}
}
here is for my score shadowing for another scene:
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class ScoreShadow : $$anonymous$$onoBehaviour
{
public Text scoreText;
public Text hiScoreText;
public float scoreCount = PlayerPrefs.GetFloat ("Score");
public int hiScoreCount = PlayerPrefs.GetFloat ("HighScore");
public bool alive;
void Start (){
alive = true;
}
public void Lips(){
scoreCount += 100;
}
public void Cheeks(){
scoreCount += 50;
}
public void Nose(){
scoreCount += 20;
}
public void ForeHead(){
scoreCount += 10;
}
public void $$anonymous$$(){
scoreCount -= 50;
}
void Update ()
{
scoreText.text = "Score: " + $$anonymous$$athf.Round (PlayerPrefs.GetFloat ("Score")+scoreCount);
hiScoreText.text = "High Score: " + $$anonymous$$athf.Round (hiScoreCount);
if ((scoreCount+PlayerPrefs.GetFloat ("Score")) > hiScoreCount) {
hiScoreCount = PlayerPrefs.GetFloat ("Score")+scoreCount;
PlayerPrefs.SetInt("HighScores", hiScoreCount);
}
}
}
I've beem trying to figure it out but i cant see what is wrong.
Answer by goutham12 · Jan 27, 2017 at 09:14 AM
first delete all your player preference using playerpreferences.deleteall()....
after check your scripts is any scrite having PlayerPrefs.SetInt("HighScores",0) in start or awake methods. may this help to you......
you are running your highscore updation code in update try to avoid that thing.
Answer by malkere · Jan 27, 2017 at 10:05 AM
PlayerPrefs is used for storing persistent data, like a high score, but not a score which is temporary. The difference being a score only gets used -while playing the game- the highscore gets saved so it can be looked at the next day or whenever. So you don't need to use
PlayerPrefs.GetInt("Score");
just keep that as a variable in your script, the
int score;
Now one thing you need to understand is why you can't do this:
public float scoreCount = PlayerPrefs.GetFloat ("Score");
You're declaring a variable when you call public float like that, and it's called initializing when you set it equal to something. That's all you're allowed to do at that point, before you get into any methods. After that you have your Start() method, inside of which you could call PlayerPrefs.GetFloat("Score"); because that is a part of the script that gets executed, or run. The scoreCount is just a variable and only gets used because it's not in a method, so you can't called execution like GetFloat there.
How about something like this: using UnityEngine; using System.Collections; using UnityEngine.UI;
public class MyClass : MonoBehaviour {
float score = 0;
Text scoreDisplay;
Text highScoreDisplay;
//you cannot call PlayerPrefs or any method here
float currentHighScore;
//declare initial values here
void Start () {
scoreDisplay = GameObject.Find("MainUI/ScoreDisplay").GetComponent<Text>();
highScoreDisplay = GameObject.Find("MainUI/HighScoreDisplay").GetComponent<Text>();
//call PlayerPrefs inside a method like Start();
currentHighScore = PlayerPrefs.GetFloat("HighScore");
highScoreDisplay.text = Mathf.RoundToInt(currentHighScore).ToString();
}
void AddToScore (int amount) {
score += amount;
//Call another method to do something for you
UpdateScore();
}
void UpdateScore () {
scoreDisplay.text = Mathf.RoundToInt(score).ToString();
//don't update the highscore when you don't need to
if (score > currentHighScore) {
PlayerPrefs.SetFloat("HighScore", score);
//try to call outside methods as little as possible by storing and reusing
currentHighScore = score;
highScoreDisplay.text = Mathf.RoundToInt(currentHighScore).ToString();
}
}
}
There are tons of different ways to do anything you can imagine. This is how I might get started writing something along these lines.