質問編集履歴

2

初心者アイコンの付加

2019/01/18 23:13

投稿

hayato208
hayato208

スコア19

test CHANGED
File without changes
test CHANGED
File without changes

1

スクリプトの追加

2019/01/18 23:13

投稿

hayato208
hayato208

スコア19

test CHANGED
File without changes
test CHANGED
@@ -22,50 +22,210 @@
22
22
 
23
23
 
24
24
 
25
+ ~~public class Door : MonoBehaviour
26
+
27
+ {
28
+
29
+ public int count;
30
+
31
+ void OnCollisionEnter(Collision other)
32
+
33
+ {
34
+
35
+ if (other.gameObject.CompareTag("Player"))
36
+
37
+
38
+
39
+ {
40
+
41
+ if (count >= 1)
42
+
43
+ {
44
+
45
+ Destroy(gameObject, 1.0f);
46
+
47
+ }
48
+
49
+ }
50
+
51
+ }
52
+
53
+ }`~~
54
+
55
+
56
+
57
+
58
+
59
+ ```編集後スクリプト
60
+
61
+ ```
62
+
25
63
  public class Door : MonoBehaviour
26
64
 
27
65
  {
28
66
 
67
+ private void Start()
68
+
69
+ {
70
+
71
+ int Dcount;
72
+
73
+ Dcount = PlayerController_Complete.count;
74
+
75
+ }
76
+
77
+
78
+
79
+ void OnCollisionEnter(Collision other)
80
+
81
+ {
82
+
83
+ if (other.gameObject.CompareTag("Player"))
84
+
85
+
86
+
87
+ {
88
+
89
+ if (Dcount >= 1)
90
+
91
+ {
92
+
93
+ Destroy(gameObject, 1.0f);
94
+
95
+ }
96
+
97
+ }
98
+
99
+ }
100
+
101
+ }
102
+
103
+ ```
104
+
105
+ ![イメージ説明](413bf10f79629f386f4a9d025a33c44f.png)
106
+
107
+ Dcountのコードを追加したところ、このようなエラーが吐き出されました。
108
+
109
+ DoorのスクリプトにPlayerのcountスクリプトを入れようと思った次第です。
110
+
111
+
112
+
113
+
114
+
115
+ ### 試したこと
116
+
117
+ 1)Doorオブジェクトのスクリプト内でcountを5にするとDoorは削除された。
118
+
119
+ 2)Playerオブジェクトのスクリプト内でcountを5にしてもDoorは削除されなかった。
120
+
121
+ 1),2)から、if構文自体に問題はないが、Player側のcountとDoor側のcountが同一のものとして扱われていない?
122
+
123
+
124
+
125
+ ### 補足情報(FW/ツールのバージョンなど)
126
+
127
+ unity2018.3.0f2
128
+
129
+ Visual Studio COmmunity 2017 ver 15.9.5
130
+
131
+
132
+
133
+ Player側のスクリプトを記載しておきます。
134
+
135
+ 回答よろしくお願いします。
136
+
137
+
138
+
139
+ ```
140
+
141
+ public class PlayerController_Complete : MonoBehaviour
142
+
143
+ {
144
+
145
+
146
+
147
+ Rigidbody rb;
148
+
149
+ public float speed;
150
+
29
151
  public int count;
30
152
 
31
- void OnCollisionEnter(Collision other)
32
-
33
- {
34
-
35
- if (other.gameObject.CompareTag("Player"))
36
-
37
-
38
-
39
- {
40
-
41
- if (count >= 1)
42
-
43
- {
44
-
45
- Destroy(gameObject, 1.0f);
46
-
47
- }
48
-
49
- }
50
-
51
- }
52
-
53
- }`
54
-
55
-
56
-
57
- ### 試したこと
58
-
59
- 1)Doorオブジェクトのスクリプト内でcountを5にするとDoorは削除された。
60
-
61
- 2)Playerオブジェクトのスクリプト内でcountを5にしてもDoorは削除されなかった。
62
-
63
- 1),2)から、if構文自体に問題はないが、Player側のcountとDoor側のcountが同一のものとして扱われていない?
64
-
65
-
66
-
67
- ### 補足情報(FW/ツールのバージョンなど)
68
-
69
- unity2018.3.0f2
70
-
71
- Visual Studio COmmunity 2017 ver 15.9.5
153
+ public Text countText;
154
+
155
+
156
+
157
+ // Use this for initialization
158
+
159
+ void Start()
160
+
161
+ {
162
+
163
+ rb = GetComponent<Rigidbody>();
164
+
165
+ count = 10;
166
+
167
+ SetCountText();
168
+
169
+ }
170
+
171
+
172
+
173
+ // Update is called once per frame
174
+
175
+ void FixedUpdate()
176
+
177
+ {
178
+
179
+ float moveH = Input.GetAxis("Horizontal");
180
+
181
+ float moveV = Input.GetAxis("Vertical");
182
+
183
+
184
+
185
+ Vector3 move = new Vector3(moveH, 0, moveV);
186
+
187
+ rb.AddForce(move * speed);
188
+
189
+ }
190
+
191
+
192
+
193
+ public void OnTriggerEnter(Collider other)
194
+
195
+ {
196
+
197
+ if (other.gameObject.CompareTag("A"))
198
+
199
+ {
200
+
201
+ other.gameObject.SetActive(false);
202
+
203
+ count = count + 1;
204
+
205
+ SetCountText();
206
+
207
+ }
208
+
209
+ else if (other.gameObject.CompareTag("B"))
210
+
211
+ {
212
+
213
+ SceneManager.LoadScene(SceneManager.GetActiveScene().name);
214
+
215
+ }
216
+
217
+ }
218
+
219
+ void SetCountText()
220
+
221
+ {
222
+
223
+ countText.text = "ゲット数:" + count.ToString();
224
+
225
+ }
226
+
227
+ }
228
+
229
+
230
+
231
+ ```