- Home /
How do I make my score counter only start when I hit the default key and stop when the player dies?
Hello everyone I need help, I am a complete novice I am creating a game with various tutorials and when I start the game I have a predetermined key for the character to start running but before I press it the score already starts running and when the player dies score keeps running how do i fix this?
this is my score code
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;
public class Score : MonoBehaviour { public Text scoreText; public float scoreAmonut; public float pointIncreased;
// Start is called before the first frame update
void Start()
{
scoreAmonut = 0f;
pointIncreased = 1f;
}
// Update is called once per frame
void Update()
{
scoreText.text = (int)scoreAmonut + "";
scoreAmonut += pointIncreased * Time.deltaTime;
}
}
Answer by Darckret · Mar 07 at 01:56 PM
@Caeser_21 Thank you very much now when you start the counter it starts when you press the key, sorry for being a complete novice in the note that says "When the player dies you have to call "StopScore()" I don't understand where I should put it.
Could you send the script that make the player die?...
If I have that I can change it to stop the counter.
Here it is, I really appreciate the help.
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Jugador : MonoBehaviour {
public float fuerzaSalto;
public GameManager gameManager;
public Transform groundCheck; // se sabe cuando el jugador esta tocando el suelo
public LayerMask ground;
public float groundCheckRadius;
private Rigidbody2D rigidbody2D;
private Animator animator;
private bool isGrounded;
public int playerJumps;
private int tempPlayerJumps;
// Start is called before the first frame update
void Start()
{
animator = GetComponent<Animator>();
rigidbody2D = GetComponent<Rigidbody2D>();
}
// Update is called once per frame
private void FixedUpdate()
{
isGrounded = Physics2D.OverlapCircle(groundCheck.position, groundCheckRadius, ground);
}
void Update()
{
if (isGrounded)
{
tempPlayerJumps = playerJumps;
}
if (Input.GetKeyDown(KeyCode.Space) && tempPlayerJumps > 0)
{
rigidbody2D.velocity = Vector2.up * fuerzaSalto;
tempPlayerJumps--;
}
if (Input.GetKeyDown(KeyCode.Space))
{
animator.SetBool("estaSaltando", true);
rigidbody2D.AddForce(new Vector2(0, fuerzaSalto));
}
}
private void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.tag == "Suelo")
{
animator.SetBool("estaSaltando", false);
}
if (collision.gameObject.tag == "Obstaculo")
{
gameManager.gameOver = true;
}
}
}
@Darckret I have updated it in my initial answer, check if it works...
Thank you friend, you really helped me a lot, I had been looking for a solution for hours and I couldn't find it, I thank you very much!!
Answer by Caeser_21 · Mar 07 at 01:42 PM
Ok so you will have to make the following changes to your script :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Score : MonoBehaviour
{
public Text scoreText;
public float scoreAmonut;
public float pointIncreased;
private bool Started;
void Start()
{
Started = false;
scoreAmonut = 0f;
pointIncreased = 1f;
}
void Update()
{
if (Input.GetKeyDown(YourKey)) //"YourKey" should be the predetermined key
{
Started = true;
}
if (Started)
{
scoreText.text = (int)scoreAmonut + "";
scoreAmonut += pointIncreased * Time.deltaTime;
}
}
public void StopScore()
{
Started = false;
}
}
And for the 'Player' script :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Jugador : MonoBehaviour
{
public float fuerzaSalto;
public GameManager gameManager;
public Score ScoreScript; //Reference this in the Hierarchy, like you did with the 'GameManager' script
public Transform groundCheck; // se sabe cuando el jugador esta
tocando el suelo
public LayerMask ground;
public float groundCheckRadius;
private Rigidbody2D rigidbody2D;
private Animator animator;
private bool isGrounded;
public int playerJumps;
private int tempPlayerJumps;
// Start is called before the first frame update
void Start()
{
animator = GetComponent<Animator>();
rigidbody2D = GetComponent<Rigidbody2D>();
}
// Update is called once per frame
private void FixedUpdate()
{
isGrounded = Physics2D.OverlapCircle(groundCheck.position, groundCheckRadius, ground);
}
void Update()
{
if (isGrounded)
{
tempPlayerJumps = playerJumps;
}
if (Input.GetKeyDown(KeyCode.Space) && tempPlayerJumps > 0)
{
rigidbody2D.velocity = Vector2.up * fuerzaSalto;
tempPlayerJumps--;
}
if (Input.GetKeyDown(KeyCode.Space))
{
animator.SetBool("estaSaltando", true);
rigidbody2D.AddForce(new Vector2(0, fuerzaSalto));
}
}
private void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.tag == "Suelo")
{
animator.SetBool("estaSaltando", false);
}
if (collision.gameObject.tag == "Obstaculo")
{
gameManager.gameOver = true;
ScoreScript.StopScore();
}
}
}
NOTE 1: You also might have to change the script according to the death screen and other stuff...
Your answer
Follow this Question
Related Questions
[C#] if statement HELP! 1 Answer
If statment not printing to console. 2 Answers
Unity string comparison not working... 1 Answer
GetComponent, int error, if statement, problem. 1 Answer
If statement not working 2 Answers