- Home /
How to I make the Score only increase not decrease?,Score for my game
Hello, I am currently developing a 2D platformer and I have Created a score. The Script I am using is: scoreText.text = player.position.x.ToString("0"); This makes a Text element display the x Position of the player. I really want to have the option for the player to be able to walk both backward and forward, but I don't want the score to decrease when The player is going backward. Is there any way that the score stays the same when The player moves back, and only Increases once it passes the current score? Thank you very much
Answer by logicandchaos · Feb 02, 2021 at 01:35 AM
Yes you have to create a seperate variable for it tho. Like float score; try this:
//in player
public float score;
//where you set your text
scoreText.text = player.score.ToString("0");
//after you move your player
if(player.score<player.position.x)
player.score=player.position.x;
Answer by Agent_Phantom8 · Feb 02, 2021 at 08:35 AM
Thank you soo much!
It doesnt work for me . . . do I need to add this into 2 separate Scripts?
This is my Script at the moment:
using UnityEngine; using UnityEngine.UI;
public class Score : $$anonymous$$onoBehaviour {
public Transform player;
public Text scoreText;
// Update is called once per frame
void Update() {
scoreText.text = player.position.x.ToString("0");
}
}
But Where do I insert you're Details?
If this bothers you, you should probably invest some time into learning program$$anonymous$$g fundamentals before diving into actual game dev. nothing wrong with depthening your understanding of program$$anonymous$$g through dabbling in game development, however trying to learn the basics through it is a task that is accomplished way faster and way more efficiently through actual program$$anonymous$$g practice outside of gamedev.
you can, to test this, add the variable that holds your score to your script below the public Text scoreText;
then you put this (slightly modified it to work in one script by eli$$anonymous$$ating player)
//after you move your player
if(score<player.position.x)
score=player.position.x;
below your line of code within Update().
But really, if you have to ask this simple of a question after being given a SUPER simple to apply answer WITH instructions, you will have to learn program$$anonymous$$g before trying to continue this path.
Thanks, Now it works. I will definitely try and learn the basics of c# better . . . still thanks a lot.
Your answer
Follow this Question
Related Questions
Help with simple issue (hopefully) 1 Answer
Score is decrementing twice while passing through a collider. 1 Answer
Need suggestion or advice on my 2d platformer 0 Answers
Noob Question -- Programming 2d platformer 1 Answer
Climbing a block in 2d 0 Answers