回答編集履歴

2

補足

2023/09/04 22:27

投稿

YAmaGNZ
YAmaGNZ

スコア10546

test CHANGED
@@ -44,6 +44,7 @@
44
44
 
45
45
 
46
46
  書くとすれば下記のような感じになるかと思います。
47
+ インスペクタ上でSmにSystemMainを設定しているのであれば、Startメソッドの中のコードは不要かもしれません。
47
48
  ```C#
48
49
  using System.Collections;
49
50
  using System.Collections.Generic;

1

追記

2023/09/04 22:25

投稿

YAmaGNZ
YAmaGNZ

スコア10546

test CHANGED
@@ -8,4 +8,82 @@
8
8
  TouchDeleteScriptにはSmは定義してありませんので、それらの定義が必要になります。
9
9
  そのあたりはスコアを加算する方法を参照すればいいでしょう。
10
10
 
11
+ ------
12
+ ### 追記
11
13
 
14
+ 修正されたコードですがいろいろと間違っています。
15
+
16
+ ```C#
17
+ if (clickedGameObject.tag == "mato")
18
+ {
19
+ Destroy(clickedGameObject);
20
+ }
21
+ {
22
+ if (Status == 0)
23
+ {
24
+ Sm.Score += 1; //スコア加算していく数字
25
+ Destroy(this.gameObject); //オブジェクトが消えた時にスコア加算
26
+ }
27
+ void Start()
28
+ {
29
+ //SyatemMainを探す
30
+ Sm = GameObject.Find("SystemMain").GetComponent<SystemMain>();
31
+ }
32
+ }
33
+ ```
34
+ この部分ですが最初のif文でclickedGameObjectのタグが"mato"だった場合にclickedGameObjectをDestroyしています。
35
+ その後、{}で囲まれたif (Status == 0)がありますが、これは無条件で実行されます。
36
+ そして、この中で`Destroy(this.gameObject);`と自分のgameObjectをDestroyしています。
37
+ このためにこのスクリプト自体が動作を停止しているため、対象を消したりスコアの加算がされなかったりするのでしょう。
38
+
39
+ また、この{}の中でStartメソッドを定義しています。
40
+ これも定義しているだけで合って実行されません。
41
+
42
+ clickedGameObject = hitSprite.transform.gameObject;
43
+ この行あたりにブレークポイントを設定してステップ実行してみればどのように動作しているのか確認できますのでやってみてください。
44
+
45
+
46
+ 書くとすれば下記のような感じになるかと思います。
47
+ ```C#
48
+ using System.Collections;
49
+ using System.Collections.Generic;
50
+ using UnityEngine;
51
+
52
+ public class TouchDeleteScript : MonoBehaviour
53
+ {
54
+ GameObject clickedGameObject;
55
+ public SystemMain Sm; //ヒエラルキーのSystemMainと紐づける
56
+ private int Status; //準備ができたかどうかを判断する変数
57
+
58
+ // Start is called before the first frame update
59
+ void Start()
60
+ {
61
+
62
+ //SyatemMainを探す
63
+ Sm = GameObject.Find("SystemMain").GetComponent<SystemMain>();
64
+ }
65
+
66
+ // Update is called once per frame
67
+ void Update()
68
+ {
69
+ if (Input.GetMouseButtonDown(0))
70
+ {
71
+
72
+ clickedGameObject = null;
73
+
74
+ Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
75
+ RaycastHit2D hitSprite = Physics2D.Raycast((Vector2)ray.origin, (Vector2)ray.direction);
76
+
77
+ if (hitSprite == true)
78
+ {
79
+ clickedGameObject = hitSprite.transform.gameObject;
80
+ if (clickedGameObject.tag == "mato")
81
+ {
82
+ Sm.Score += 1; //スコア加算していく数字
83
+ Destroy(clickedGameObject);
84
+ }
85
+ }
86
+ }
87
+ }
88
+ }
89
+ ```