-
[Unity] Score 초기화 및 Best ScoreUnity/게임 만들어보기 기록장 2022. 10. 24. 17:18728x90
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'Unity > 게임 만들어보기 기록장' 카테고리의 다른 글
[Unity] 효과음 추가하기 (1) 2022.10.25 [Unity] 너무 높게나는거 방지하기 (0) 2022.10.24 [Unity]GameOver Scene만들기 (0) 2022.10.23 [Unity] Score 스크립트 만들기 (0) 2022.10.22 [Unity] Prefab을 이용하여 파이프 만들기 (0) 2022.10.22