몬스터 캐릭터가 각각의 점수(+10점, +50점, -30점)의 코인을 먹을 때마다 화면 상단의 UI - Text를 이용하여 스코어보드 점수 증가 & 감소하여 저장하는 방법.
[MonsterMove.cs]
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MonsterMove : MonoBehaviour
{
Rigidbody2D rb;
float moveX, moveY;
[SerializeField] float speed = 7f;
// Start is called before the first frame update
void Start()
{
rb = GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void Update()
{
moveX = Input.GetAxis("Horizontal");
moveY = Input.GetAxis("Vertical");
rb.velocity = new Vector2(moveX * speed, moveY * speed);
}
private void OnTriggerEnter2D(Collider2D collision)
{
switch (collision.gameObject.name)
{
case "Coin+10":
ScoreText.scoreValue += 10;
break;
case "Coin+50":
ScoreText.scoreValue += 50;
break;
case "Coin-30":
ScoreText.scoreValue -= 30;
break;
}
}
}
[ScoreText.cs]
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class ScoreText : MonoBehaviour
{
Text text;
public static int scoreValue;
// Start is called before the first frame update
void Start()
{
text = GetComponent<Text>();
}
// Update is called once per frame
void Update()
{
text.text = "Score : " + scoreValue;
}
}
댓글