Unityの2Dアクションゲームにて、無限湧きの敵を撃つごとにスコア加算をしていきたいが、Prefabどうしだとスクリプトの参照ができず思い通りの挙動が確認できません。
PrefabどうしのOnTriggerEnter2D内でScoreスクリプトを参照するにはどうすればよいですか?
c#
1Score.cs 2 3using System.Collections; 4using System.Collections.Generic; 5using UnityEngine; 6using UnityEngine.UI; 7using UnityEngine.SceneManagement; 8 9public class Score : MonoBehaviour 10{ 11 /* public変数 */ 12 public static int scoreA, scoreB, scoreC; 13 14 /* private変数 */ 15 Text scoreTextA, scoreTextB, scoreTextC; 16 17 void Awake() 18 { 19 if (SceneManager.GetActiveScene().name == "Main" && SceneManager.GetActiveScene().name != "Main2" && SceneManager.GetActiveScene().name != "Main3") 20 { 21 scoreA = 0; 22 scoreTextA = GetComponent<Text>(); 23 } 24 25 else if (SceneManager.GetActiveScene().name != "Main" && SceneManager.GetActiveScene().name == "Main2" && SceneManager.GetActiveScene().name != "Main3") 26 { 27 scoreB = 0; 28 scoreTextB = GetComponent<Text>(); 29 } 30 31 else if (SceneManager.GetActiveScene().name != "Main" && SceneManager.GetActiveScene().name != "Main2" && SceneManager.GetActiveScene().name == "Main3") 32 { 33 scoreC = 0; 34 scoreTextC = GetComponent<Text>(); 35 } 36 } 37 38 void Update() 39 { 40 if (SceneManager.GetActiveScene().name == "Main" && SceneManager.GetActiveScene().name != "Main2" && SceneManager.GetActiveScene().name != "Main3") 41 { 42 scoreTextA.text = $"Score : { scoreA.ToString("0000") }"; //「score : ~~」表記 43 } 44 45 else if (SceneManager.GetActiveScene().name != "Main" && SceneManager.GetActiveScene().name == "Main2" && SceneManager.GetActiveScene().name != "Main3") 46 { 47 scoreTextB.text = $"Score : { scoreB.ToString("0000") }"; //「score : ~~」表記 48 } 49 50 else if (SceneManager.GetActiveScene().name != "Main" && SceneManager.GetActiveScene().name != "Main2" && SceneManager.GetActiveScene().name == "Main3") 51 { 52 scoreTextC.text = $"Score : { scoreC.ToString("0000") }"; //「score : ~~」表記 53 } 54 } 55 56 public static int GetScoreA() 57 { 58 return scoreA; 59 } 60 61 public static int GetScoreB() 62 { 63 return scoreB; 64 } 65 66 public static int GetScoreC() 67 { 68 return scoreC; 69 } 70 71 public void ScorePlus() 72 { 73 if (SceneManager.GetActiveScene().name == "Main" && SceneManager.GetActiveScene().name != "Main2" && SceneManager.GetActiveScene().name != "Main3") 74 { 75 scoreA += 100; 76 } 77 78 else if (SceneManager.GetActiveScene().name != "Main" && SceneManager.GetActiveScene().name == "Main2" && SceneManager.GetActiveScene().name != "Main3") 79 { 80 scoreB += 120; 81 } 82 83 else if (SceneManager.GetActiveScene().name != "Main" && SceneManager.GetActiveScene().name != "Main2" && SceneManager.GetActiveScene().name == "Main3") 84 { 85 scoreC += 150; 86 } 87 } 88}
c#
1EnemyMove.cs 2 3using System.Collections; 4using System.Collections.Generic; 5using UnityEngine; 6 7public class EnemyMove : MonoBehaviour 8{ 9 /* public変数 */ 10 [Header("移動速度")] public float speed; 11 [Header("周期")] public float thetaScale; 12 [Header("高低差")] public float sinScale; 13 public Score score; 14 15 /* private変数 */ 16 Vector3 pos; 17 float theta; 18 19 void Start() 20 { 21 score = GameObject.Find 22 23 pos = transform.position; 24 } 25 26 void FixedUpdate() 27 { 28 pos = transform.position; 29 30 pos.x -= 0.05f * speed; 31 32 theta += Time.deltaTime * thetaScale; 33 pos.y = Mathf.Sin(theta) * sinScale; 34 35 transform.position = new Vector3(pos.x, pos.y, -3); 36 37 if (pos.x < -12.0f) 38 { 39 Destroy(gameObject); 40 } 41 } 42 43}
c#
1BulletMove.cs 2 3using System.Collections; 4using System.Collections.Generic; 5using UnityEngine; 6 7public class BulletMove : MonoBehaviour 8{ 9 void Start() 10 { 11 Destroy(this.gameObject, 1.5f); 12 } 13 14 void Update() 15 { 16 17 } 18 19 private void OnTriggerEnter2D(Collider2D collision) 20 { 21 if(collision.tag == "Enemy") 22 { 23 Destroy(collision.gameObject); 24 } 25 } 26}
ちなみに、Bulletの移動速度そのものはPlayerのほうで管理しています。
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2021/06/30 09:18