ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [Unity] Score 초기화 및 Best Score
    Unity/게임 만들어보기 기록장 2022. 10. 24. 17:18
    728x90

    BirdJump

    if문으로 score와 best score를 비교해줬다.

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    
    using UnityEngine.SceneManagement;
    
    public class BirdJump : MonoBehaviour
    {
        Rigidbody2D rb;
        public float jumpPower;
        // Start is called before the first frame update
        void Start()
        {
            rb = GetComponent<Rigidbody2D>();
        }
    
        // Update is called once per frame
        void Update()
        {
            if (Input.GetMouseButtonDown(0))
            {
                rb.velocity = Vector2.up * jumpPower; //(0,3)
            }
        }
    
    //충돌시 게임오버 
        private void OnCollisionEnter2D(Collision2D other) {
            if(Score.score > Score.bestScore){
                Score.bestScore = Score.score;
            }
            SceneManager.LoadScene("GameOverScene");
    
        }
    }

     

    score

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.UI;
    
    public class Score : MonoBehaviour
    {
        public static int score = 0;
        public static int bestScore = 0;
        // Start is called before the first frame update
        void Start()
        {
            score = 0;
        }
    
        // Update is called once per frame
        void Update()
        {
            GetComponent<Text>().text = score.ToString();
        }
    }

     

    CurrentScore

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.UI;
    
    public class CurrentScore : MonoBehaviour
    {
        // Start is called before the first frame update
        void Start()
        {
            GetComponent<Text>().text = "Score: "+Score.score.ToString();
        }
    }

     

    BestScore

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.UI;
    
    public class BestScore : MonoBehaviour
    {
        // Start is called before the first frame update
        void Start()
        {
            GetComponent<Text>().text = "Best Score: " + Score.bestScore;
        }
    }
    728x90

    댓글

Designed by Tistory.