スコアの獲得と表示
![030a8278 9396 4e67 a5a4 59529381e371](https://codegenius.org/uploads/slide/image/7630/030a8278-9396-4e67-a5a4-59529381e371.jpeg)
![5ae31c6f 20d8 4a38 9941 eb367da2ada1](https://codegenius.org/uploads/slide/image/7631/5ae31c6f-20d8-4a38-9941-eb367da2ada1.jpeg)
![F32fc056 d3eb 496b a2cb 34bdfe9e64ee](https://codegenius.org/uploads/slide/image/7632/f32fc056-d3eb-496b-a2cb-34bdfe9e64ee.jpeg)
ScoreManager
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
// ★追加
using TMPro;
public class ScoreManager : MonoBehaviour
{
public TextMeshProUGUI scoreLabel;
private int score;
void Start()
{
// 0埋めのテクニック(0を何個並べるかは自由。ここでは15個)
scoreLabel.text = "" + score.ToString("D15");
}
// メソッド経由でscoreの値を変更する。
public void AddScore(int amount)
{
score += amount;
scoreLabel.text = "" + score.ToString("D15");
}
}
![36b4a8a1 c273 472a 80de 2f25717b4e26](https://codegenius.org/uploads/slide/image/7633/36b4a8a1-c273-472a-80de-2f25717b4e26.jpeg)
![8388fd84 94ae 441e aed0 c0d22a2a7a64](https://codegenius.org/uploads/slide/image/7634/8388fd84-94ae-441e-aed0-c0d22a2a7a64.jpeg)
スーパークラスにスコア増加の機能を実装する
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public abstract class EnemyBase : MonoBehaviour
{
public int HP;
// ★追加(スコア)
public int ScoreValue;
public virtual void TakeDamage(int missilePower)
{
HP -= missilePower;
if (HP < 1)
{
Destroy(gameObject);
// ★追加(スコア)
GameObject.Find("ScoreManager").GetComponent<ScoreManager>().AddScore(ScoreValue);
}
}
}
![C83049b8 57dc 4492 beb6 7673a968a8ae](https://codegenius.org/uploads/slide/image/7635/c83049b8-57dc-4492-beb6-7673a968a8ae.jpeg)
スコアの機能の追加
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MoveEnemyA : EnemyBase
{
private float x;
private float z;
private float speed;
void Start()
{
HP = 1;
StartCoroutine(MoveE());
// ★追加(スコア)
ScoreValue = 100;
}
void Update()
{
transform.Translate(new Vector3(x, 0, z) * speed * Time.deltaTime, Space.World);
}
private IEnumerator MoveE()
{
x = 0;
z = -1;
speed = 5;
yield return new WaitForSeconds(3f);
x = 1;
z = 0;
speed = 10;
}
}
![9de4a7d5 87d2 4589 8589 0a2d20d179d8](https://codegenius.org/uploads/slide/image/7636/9de4a7d5-87d2-4589-8589-0a2d20d179d8.jpeg)
【2021版】Danmaku(基礎/全55回)
他のコースを見る![030a8278 9396 4e67 a5a4 59529381e371](https://codegenius.org/uploads/slide/image/7630/030a8278-9396-4e67-a5a4-59529381e371.jpeg)
![5ae31c6f 20d8 4a38 9941 eb367da2ada1](https://codegenius.org/uploads/slide/image/7631/5ae31c6f-20d8-4a38-9941-eb367da2ada1.jpeg)
![F32fc056 d3eb 496b a2cb 34bdfe9e64ee](https://codegenius.org/uploads/slide/image/7632/f32fc056-d3eb-496b-a2cb-34bdfe9e64ee.jpeg)
ScoreManager
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
// ★追加
using TMPro;
public class ScoreManager : MonoBehaviour
{
public TextMeshProUGUI scoreLabel;
private int score;
void Start()
{
// 0埋めのテクニック(0を何個並べるかは自由。ここでは15個)
scoreLabel.text = "" + score.ToString("D15");
}
// メソッド経由でscoreの値を変更する。
public void AddScore(int amount)
{
score += amount;
scoreLabel.text = "" + score.ToString("D15");
}
}
![36b4a8a1 c273 472a 80de 2f25717b4e26](https://codegenius.org/uploads/slide/image/7633/36b4a8a1-c273-472a-80de-2f25717b4e26.jpeg)
![8388fd84 94ae 441e aed0 c0d22a2a7a64](https://codegenius.org/uploads/slide/image/7634/8388fd84-94ae-441e-aed0-c0d22a2a7a64.jpeg)
スーパークラスにスコア増加の機能を実装する
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public abstract class EnemyBase : MonoBehaviour
{
public int HP;
// ★追加(スコア)
public int ScoreValue;
public virtual void TakeDamage(int missilePower)
{
HP -= missilePower;
if (HP < 1)
{
Destroy(gameObject);
// ★追加(スコア)
GameObject.Find("ScoreManager").GetComponent<ScoreManager>().AddScore(ScoreValue);
}
}
}
![C83049b8 57dc 4492 beb6 7673a968a8ae](https://codegenius.org/uploads/slide/image/7635/c83049b8-57dc-4492-beb6-7673a968a8ae.jpeg)
スコアの機能の追加
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MoveEnemyA : EnemyBase
{
private float x;
private float z;
private float speed;
void Start()
{
HP = 1;
StartCoroutine(MoveE());
// ★追加(スコア)
ScoreValue = 100;
}
void Update()
{
transform.Translate(new Vector3(x, 0, z) * speed * Time.deltaTime, Space.World);
}
private IEnumerator MoveE()
{
x = 0;
z = -1;
speed = 5;
yield return new WaitForSeconds(3f);
x = 1;
z = 0;
speed = 10;
}
}
![9de4a7d5 87d2 4589 8589 0a2d20d179d8](https://codegenius.org/uploads/slide/image/7636/9de4a7d5-87d2-4589-8589-0a2d20d179d8.jpeg)
スコアの獲得と表示