Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Darckret · Mar 07 at 01:29 PM · if statement

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;
 }

}

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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.

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Caeser_21 · Mar 07 at 02:01 PM 0
Share

Could you send the script that make the player die?...
If I have that I can change it to stop the counter.

avatar image Darckret · Mar 07 at 02:02 PM 0
Share

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;
     }
 }

}

avatar image Caeser_21 Darckret · Mar 07 at 02:14 PM 0
Share

@Darckret I have updated it in my initial answer, check if it works...

avatar image Darckret · Mar 07 at 02:21 PM 0
Share

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!!

avatar image Caeser_21 Darckret · Mar 07 at 02:29 PM 0
Share

Np mate...

avatar image
0

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...

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

132 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges