敵に得点を付けて画面にスコア表示





スコアを表示する
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
// ★追加
using TMPro;
public class ScoreManager : MonoBehaviour
{
private int score = 0;
private TextMeshProUGUI scoreLabel;
void Start()
{
scoreLabel = GetComponent<TextMeshProUGUI>();
scoreLabel.text = "" + score;
}
// (復習)
// スコアを増加させるメソッド
// 外部からアクセスするためpublicをつけること。
public void AddScore(int amount)
{
score += amount;
scoreLabel.text = "" + score;
}
}


敵を破壊すると得点になる
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DestroyObject : MonoBehaviour
{
public GameObject effectPrefab;
public GameObject effectPrefab2;
public int objectHP;
public GameObject[] itemPrefabs;
// ★追加
public int scoreValue; // 得点数は自由に設定しましょう。
private ScoreManager sm;
// ★追加
private void Start()
{
sm = GameObject.Find("ScoreLabel").GetComponent<ScoreManager>();
}
private void OnTriggerEnter(Collider other)
{
if (other.CompareTag("Shell"))
{
objectHP -= 1;
if (objectHP > 0)
{
Destroy(other.gameObject);
GameObject effect = Instantiate(effectPrefab, other.transform.position, Quaternion.identity);
Destroy(effect, 2.0f);
}
else
{
Destroy(other.gameObject);
GameObject effect2 = Instantiate(effectPrefab2, other.transform.position, Quaternion.identity);
Destroy(effect2, 2.0f);
Destroy(this.gameObject);
// ★追加
sm.AddScore(scoreValue);
int itemNumber = Random.Range(0, 100);
if(itemPrefabs.Length != 0)
{
Vector3 pos = transform.position;
if(itemNumber < 10)
{
Instantiate(itemPrefabs[0], new Vector3(pos.x, pos.y + 0.6f, pos.z), Quaternion.identity);
}
else if (itemNumber < 40)
{
Instantiate(itemPrefabs[1], new Vector3(pos.x, pos.y + 0.6f, pos.z), Quaternion.identity);
}
else
{
Instantiate(itemPrefabs[2], new Vector3(pos.x, pos.y + 0.6f, pos.z), Quaternion.identity);
}
}
}
}
}
}


【2021版】BattleTank(基礎/全33回)
他のコースを見る




スコアを表示する
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
// ★追加
using TMPro;
public class ScoreManager : MonoBehaviour
{
private int score = 0;
private TextMeshProUGUI scoreLabel;
void Start()
{
scoreLabel = GetComponent<TextMeshProUGUI>();
scoreLabel.text = "" + score;
}
// (復習)
// スコアを増加させるメソッド
// 外部からアクセスするためpublicをつけること。
public void AddScore(int amount)
{
score += amount;
scoreLabel.text = "" + score;
}
}


敵を破壊すると得点になる
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DestroyObject : MonoBehaviour
{
public GameObject effectPrefab;
public GameObject effectPrefab2;
public int objectHP;
public GameObject[] itemPrefabs;
// ★追加
public int scoreValue; // 得点数は自由に設定しましょう。
private ScoreManager sm;
// ★追加
private void Start()
{
sm = GameObject.Find("ScoreLabel").GetComponent<ScoreManager>();
}
private void OnTriggerEnter(Collider other)
{
if (other.CompareTag("Shell"))
{
objectHP -= 1;
if (objectHP > 0)
{
Destroy(other.gameObject);
GameObject effect = Instantiate(effectPrefab, other.transform.position, Quaternion.identity);
Destroy(effect, 2.0f);
}
else
{
Destroy(other.gameObject);
GameObject effect2 = Instantiate(effectPrefab2, other.transform.position, Quaternion.identity);
Destroy(effect2, 2.0f);
Destroy(this.gameObject);
// ★追加
sm.AddScore(scoreValue);
int itemNumber = Random.Range(0, 100);
if(itemPrefabs.Length != 0)
{
Vector3 pos = transform.position;
if(itemNumber < 10)
{
Instantiate(itemPrefabs[0], new Vector3(pos.x, pos.y + 0.6f, pos.z), Quaternion.identity);
}
else if (itemNumber < 40)
{
Instantiate(itemPrefabs[1], new Vector3(pos.x, pos.y + 0.6f, pos.z), Quaternion.identity);
}
else
{
Instantiate(itemPrefabs[2], new Vector3(pos.x, pos.y + 0.6f, pos.z), Quaternion.identity);
}
}
}
}
}
}


敵に得点を付けて画面にスコア表示