#前提・実現したいこと
barオブジェクトがCube_Backオブジェクトに衝突したらscoreNumに10加算していき、それをスコア表示しようとしております。
Debug.Logで確認すると、ずっと10のままでした。何が原因でしょうか?
Destroy関数は正常に処理されています。
##Script
C#
1public class BarObstScript : MonoBehaviour 2{ 3 public float bar_speed; 4 int scoreNum = 0; //score用変数 5 6 void Start() 7 { 8 transform.localScale = new Vector3(1f, 1f, Random.Range(3f, 6f)); 9 } 10 11 void Update() 12 { 13 transform.position += new Vector3(bar_speed * Time.deltaTime, 0f, 0f); 14 } 15 16 private void OnTriggerEnter(Collider other) 17 { 18 if (other.gameObject.CompareTag("Cube_Back")) 19 { 20 scoreNum += 10; 21 Debug.Log(scoreNum); 22 Destroy(gameObject); 23 } 24 } 25}
##Script2
原因がありそうな他のスクリプトは下記ぐらいです。
こちらはプレハブ化したbarオブジェクトを一定間隔で生成しようとしてます。
C#
1public class BarObstReplicScript : MonoBehaviour 2{ 3 public GameObject BarObst; 4 5 void Start() 6 { 7 InvokeRepeating("SpawnBar", 0f, 1.4f); 8 } 9 10 void Update() 11 { 12 13 } 14 15 void SpawnBar() 16 { 17 Instantiate(BarObst, new Vector3(transform.position.x, transform.position.y, transform.position.z), transform.rotation); 18 } 19}
###開発環境
macOS Mojave 10.14.4(18E226)
Unity 2018.3.11f1
Visual Studio 7.8.3.2