0
\$\begingroup\$

I want my game to start after a "3-2-1 go" countdown.

My Player Script :

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class Player : MonoBehaviour {


    private Rigidbody2D myRigidbody;

    private Animator myAnimator;
    private int skor;
    public Text toplamSkor;
    public Text highScore;





    [SerializeField]
    private float hiz;
    private bool sagaBak;

    [SerializeField]
    private AudioSource altinSes;


    // Use this for initialization
    void Start () {
        sagaBak = true;
        skor = 0;
        highScore.text = PlayerPrefs.GetInt ("HighScore", 0).ToString();



        myRigidbody = GetComponent<Rigidbody2D> ();
        myAnimator = GetComponent<Animator> ();

    }

    // Update is called once per frame
    void Update () {

        float yatay = Input.GetAxis ("Horizontal");
        TemelHareketler (yatay);
        YonCevir (yatay);

    }

    private void TemelHareketler (float yatay){
        myRigidbody.velocity = new Vector2 (yatay *hiz, myRigidbody.velocity.y);
        myAnimator.SetFloat ("KarakterHizi", Mathf.Abs (yatay));

    }
    private void YonCevir (float yatay)
    {

        if (yatay > 0 && !sagaBak || yatay < 0 && sagaBak) {

            sagaBak = !sagaBak;
            Vector3 yon = transform.localScale;
            yon.x *= -1;

            transform.localScale = yon;
        }
    }
    void OnCollisionEnter2D(Collision2D other) {

        if (other.gameObject.tag == "altin") {
            other.gameObject.SetActive (false);
            altinSes.Play (); 
            skor = skor + 1;
            SkorAyarla (skor);

            if (skor > PlayerPrefs.GetInt ("HighScore", 0)) 
            {

                PlayerPrefs.SetInt ("HighScore", skor);
                highScore.text = skor.ToString();

            }
        }
}
    void SkorAyarla(int count)
    {
        toplamSkor.text = count.ToString ();
    }

    public void Reset (){
        PlayerPrefs.DeleteAll ();


    }
}
\$\endgroup\$
  • \$\begingroup\$ Is your problem in the presentation/playing of the countdown, or in pausing the game until the countdown is finished, or something else? What have you tried so far? At a scan of the code you've included, none of it looks like it's attempting to work with a count down - did you paste different code than you meant to? Or did you want help with something that's in this specific code? (And if so, what part of this code needs work?) \$\endgroup\$ – DMGregory Jan 12 at 12:22
  • \$\begingroup\$ my problem is that I don't want the game to get started right away. I want a 3-second countdown, so start after 3 seconds. I want a code example of how to do this. \$\endgroup\$ – Yusuf Yüzbaşıoğlu Jan 12 at 12:26
  • \$\begingroup\$ So naturally you searched Google and checked out the code examples already available on the first page of results as your first step, right? Did you run into any specific trouble putting these examples into practice? \$\endgroup\$ – DMGregory Jan 12 at 12:32
5
\$\begingroup\$

First you need something to show the countdown in. This is a job for the UI system. A canvas with a simple text field in the center would do:

timer canvas

Now you need a controller for that text field which changes the text from "3" to "2" to "1" to "Go" in intervals of 1 second each. A good way to do timed events like this is a coroutine which you start at the beginning. Here is an example of a CountdownController script:

using UnityEngine.UI;
using UnityEngine;
using System.Collections;

public class CountdownController : MonoBehaviour
{
    public Text countdownTextField;

    void Start()
    {
        StartCoroutine(CountdownCoroutine());
    }

    IEnumerator CountdownCoroutine() {
        countdownTextField.text = "3";
        yield return new WaitForSeconds(1.0f);
        countdownTextField.text = "2";
        yield return new WaitForSeconds(1.0f);
        countdownTextField.text = "1";
        yield return new WaitForSeconds(1.0f);
        countdownTextField.text = "Go!";
        // start the game here
        yield return new WaitForSeconds(1.0f);
        countdownTextField.text = "";
        yield return null;
    }

}

Add this script to any persistent game object in your scene (I would either recommend the canvas or the text field itself) and then drag the text field to the "Countdown Text Field" of the script in the inspector:

inspector

When you now start the game, the timer text should count down and disappear (technically it doesn't disappear. It is still there, it just shows an empty string).

You might notice that this script shows the countdown but doesn't actually do anything on on "Go!". I just added a comment "Start the game here".

The reason why I omitted this is that depending on your game you likely need to do far more than just unlock the player controlls. Start the enemy controllers, enable game physics, initialize timers etc.. Maybe you don't even want to spawn the player prefab before the countdown reached zero. You need to know what "starting the game" actually means for your specific game and program it yourself.

\$\endgroup\$
  • \$\begingroup\$ What should I do to freeze the game for 3 seconds and then start? \$\endgroup\$ – Yusuf Yüzbaşıoğlu Jan 12 at 13:03
  • 2
    \$\begingroup\$ @YusufYüzbaşıoğlu I told you in the last paragraph that there is no solution which applies to every game. So you need to use your own expertise as a professional game developer here. \$\endgroup\$ – Philipp Jan 12 at 13:11
  • \$\begingroup\$ @YusufYüzbaşıoğlu look into game states and FMS. \$\endgroup\$ – Sidar Jan 12 at 14:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.