質問編集履歴
2
ゲーム部分のコードを全部載せました
title
CHANGED
File without changes
|
body
CHANGED
@@ -1,5 +1,6 @@
|
|
1
1
|
Unity2020でリズムゲームを作ろうと考えており、時間を計るのにTime.timeの差を利用しようと思っています。
|
2
2
|
しかし、スクリプト内のOnEnable関数で代入したはずの変数stがClickButton関数の変数stに反映されず0になってしまいます。
|
3
|
+
こちらはリズム部分のスクリプトです。
|
3
4
|
```C#
|
4
5
|
|
5
6
|
using System.Collections;
|
@@ -95,7 +96,40 @@
|
|
95
96
|
}
|
96
97
|
|
97
98
|
```
|
99
|
+
こちらは上のスクリプトなどを動かす大元のスクリプトです
|
100
|
+
```c#
|
101
|
+
using System.Collections;
|
102
|
+
using System.Collections.Generic;
|
103
|
+
using UnityEngine;
|
98
104
|
|
105
|
+
public class gamedirsc : MonoBehaviour
|
106
|
+
{
|
107
|
+
public int miss = 30;
|
108
|
+
public float now;
|
109
|
+
[SerializeField] rythm1 rythm1;
|
110
|
+
// Start is called before the first frame update
|
111
|
+
void Start()
|
112
|
+
{
|
113
|
+
|
114
|
+
}
|
115
|
+
|
116
|
+
|
117
|
+
|
118
|
+
|
119
|
+
// Update is called once per frame
|
120
|
+
void Update()
|
121
|
+
{
|
122
|
+
if (Input.GetMouseButtonDown(2))
|
123
|
+
{
|
124
|
+
now = Time.time;
|
125
|
+
rythm1.enabled = true;
|
126
|
+
}
|
127
|
+
}
|
128
|
+
|
129
|
+
|
130
|
+
}
|
131
|
+
|
132
|
+
```
|
99
133
|
このコードではスクリプトが開始された時の時間とボタンを押したときの時間の差を求めています。
|
100
134
|
ですがClickbuttonのstがなぜか0になってしまうため時間差が求められません。また、deltaTimeを利用しようとしてもUpdate関数で代入したはずの変数も0になってしまいました。
|
101
135
|
ボタンをクリックした時に呼び出される関数ではほかの変数を参照することができないのでしょうか。
|
1
音を流すコードを全部載せました
title
CHANGED
File without changes
|
body
CHANGED
@@ -1,35 +1,103 @@
|
|
1
1
|
Unity2020でリズムゲームを作ろうと考えており、時間を計るのにTime.timeの差を利用しようと思っています。
|
2
|
-
しかし、スクリプト内のOnEnable関数で代入したはずの変数stがClickButton関数の変数stに反映されず0になってしまいます。
|
2
|
+
しかし、スクリプト内のOnEnable関数で代入したはずの変数stがClickButton関数の変数stに反映されず0になってしまいます。
|
3
|
+
```C#
|
3
4
|
|
5
|
+
using System.Collections;
|
6
|
+
using System.Collections.Generic;
|
7
|
+
using UnityEngine;
|
8
|
+
using UnityEngine.UI;
|
9
|
+
|
10
|
+
public class rythm1 : MonoBehaviour
|
11
|
+
{
|
12
|
+
[SerializeField] GameObject panbutton;
|
13
|
+
[SerializeField] GameObject lettucebutton;
|
14
|
+
[SerializeField] GameObject patebutton;
|
15
|
+
|
16
|
+
[SerializeField] Button panb;
|
17
|
+
|
18
|
+
Vector2 leftup = new Vector2(-270, -450);
|
19
|
+
Vector2 leftbottom = new Vector2(-270, -770);
|
20
|
+
Vector2 rightup = new Vector2(270, -450);
|
21
|
+
Vector2 rightbottom = new Vector2(270, -770);
|
22
|
+
|
23
|
+
[SerializeField] AudioSource audiosource;
|
24
|
+
[SerializeField] AudioClip clip;
|
25
|
+
|
26
|
+
float st;
|
27
|
+
|
28
|
+
[SerializeField] gamedirsc da;
|
29
|
+
|
30
|
+
[SerializeField] GameObject canvas;
|
31
|
+
GameObject pan;
|
32
|
+
GameObject lettuce;
|
33
|
+
GameObject pate;
|
34
|
+
|
4
35
|
private void OnEnable()
|
5
36
|
{
|
37
|
+
|
38
|
+
pan = (GameObject)Instantiate(panbutton);
|
39
|
+
pan.transform.SetParent(canvas.transform,false);
|
40
|
+
lettuce = (GameObject)Instantiate(lettucebutton);
|
41
|
+
lettuce.transform.SetParent(canvas.transform, false);
|
42
|
+
pate = (GameObject)Instantiate(patebutton);
|
43
|
+
pate.transform.SetParent(canvas.transform, false);
|
44
|
+
|
6
45
|
st = Time.time;
|
7
46
|
|
8
47
|
audiosource.PlayOneShot(clip);
|
48
|
+
StartCoroutine("coroutine");
|
9
49
|
|
10
50
|
Debug.Log(st);
|
11
51
|
}
|
52
|
+
|
53
|
+
private IEnumerator coroutine()
|
54
|
+
{
|
55
|
+
yield return new WaitForSeconds(5.33f);
|
56
|
+
Destroy(pan);
|
57
|
+
Destroy(lettuce);
|
58
|
+
Destroy(pate);
|
59
|
+
enabled = false;
|
60
|
+
}
|
61
|
+
|
62
|
+
// Start is called before the first frame update
|
63
|
+
void Start()
|
64
|
+
{
|
12
65
|
|
66
|
+
}
|
67
|
+
|
13
|
-
public void
|
68
|
+
public void ClickPan()
|
14
69
|
{
|
70
|
+
Debug.Log(st);
|
15
71
|
var beat = Time.time;
|
16
|
-
var don = beat - st;
|
72
|
+
var don = beat - this.st;
|
17
|
-
|
73
|
+
|
18
|
-
Debug.Log(beat);
|
19
|
-
Debug.Log(st);
|
20
74
|
if ((don >= 2.46f & don <= 2.86f)|(don >= 4.46f & don <= 4.86f))
|
21
75
|
{
|
22
76
|
Debug.Log("Great!");
|
77
|
+
Debug.Log(don);
|
23
78
|
}
|
24
79
|
else
|
25
80
|
{
|
26
81
|
|
27
82
|
Debug.Log("Miss!");
|
83
|
+
Debug.Log(don);
|
28
84
|
}
|
29
85
|
|
86
|
+
}
|
87
|
+
// Update is called once per frame
|
88
|
+
void Update()
|
89
|
+
{
|
90
|
+
if (Input.GetMouseButtonDown(0))
|
91
|
+
{
|
92
|
+
Debug.Log(da);
|
93
|
+
}
|
94
|
+
}
|
95
|
+
}
|
96
|
+
|
30
97
|
```
|
31
98
|
|
32
99
|
このコードではスクリプトが開始された時の時間とボタンを押したときの時間の差を求めています。
|
33
100
|
ですがClickbuttonのstがなぜか0になってしまうため時間差が求められません。また、deltaTimeを利用しようとしてもUpdate関数で代入したはずの変数も0になってしまいました。
|
34
101
|
ボタンをクリックした時に呼び出される関数ではほかの変数を参照することができないのでしょうか。
|
102
|
+
僕の想像では、プレハブ化して生成されたボタンであることが原因だと思っているのですが。
|
35
103
|
初めての質問でおかしな書き方になってしまい申し訳ありません。
|