質問編集履歴
2
コード編集
title
CHANGED
File without changes
|
body
CHANGED
@@ -10,7 +10,6 @@
|
|
10
10
|
[SerializeField]
|
11
11
|
float Jump=400;//ジャンプ力
|
12
12
|
public Sprite[] SPR_LIST;//アニメーション用スプライトの保持
|
13
|
-
public GameObject gameMgr;//ゲーム管理
|
14
13
|
public GameObject sc;
|
15
14
|
|
16
15
|
Rigidbody2D _rigidbody;//物理挙動
|
1
コード編集
title
CHANGED
File without changes
|
body
CHANGED
@@ -2,13 +2,85 @@
|
|
2
2
|
「NullReferenceException: Object reference not set to an instance of an object」というエラーがでてスコアがとまりません助言をいただきたいです。
|
3
3
|
```ここに言語を入力
|
4
4
|
コード (プレイヤーのscript。衝突時にゲームオーバーになりスコアを止めたい)
|
5
|
+
public class PlayerControll : MonoBehaviour {
|
6
|
+
//スプライト番号
|
7
|
+
const int SPR_FALL = 0;
|
8
|
+
const int SPR_JUMP = 1;
|
9
|
+
|
10
|
+
[SerializeField]
|
11
|
+
float Jump=400;//ジャンプ力
|
12
|
+
public Sprite[] SPR_LIST;//アニメーション用スプライトの保持
|
13
|
+
public GameObject gameMgr;//ゲーム管理
|
14
|
+
public GameObject sc;
|
15
|
+
|
16
|
+
Rigidbody2D _rigidbody;//物理挙動
|
5
|
-
|
17
|
+
SpriteRenderer _renderer;//スプライト描画
|
18
|
+
Score _sc;//ゲーム管理スクリプト
|
19
|
+
|
20
|
+
// Start is called before the first frame update
|
21
|
+
void Start()
|
6
22
|
{
|
23
|
+
_rigidbody = GetComponent<Rigidbody2D>();//物理挙動のコンポーネント獲得
|
24
|
+
_renderer = GetComponent<SpriteRenderer>();//スプライト描画コンポーネント獲得
|
25
|
+
|
26
|
+
_sc=sc.GetComponent<Score>();//ゲーム管理スクリプトを取得
|
27
|
+
}
|
28
|
+
|
29
|
+
// Update is called once per frame
|
30
|
+
void Update()
|
31
|
+
{
|
32
|
+
if (Input.GetMouseButtonDown(0))
|
33
|
+
{
|
34
|
+
GetComponent<AudioSource>().Play();
|
35
|
+
_rigidbody.velocity = Vector2.zero;//落下速度を一度リセット
|
36
|
+
_rigidbody.AddForce(new Vector2(0, Jump));//上方向の力
|
37
|
+
}
|
38
|
+
}
|
39
|
+
//固定フレーム更新
|
40
|
+
private void FixedUpdate()
|
41
|
+
{
|
42
|
+
Vector3 position = transform.position;
|
43
|
+
//画面の外から出ないようにする
|
44
|
+
float y = transform.position.y;
|
45
|
+
float vx = _rigidbody.velocity.x;
|
46
|
+
if (y > GetTop())
|
47
|
+
{
|
48
|
+
_rigidbody.velocity = Vector2.zero;//一度速度をリセット
|
49
|
+
position.y = GetTop();
|
50
|
+
}
|
51
|
+
if (y < GetBottom())
|
52
|
+
{
|
53
|
+
//下に落ちたらオートジャンプ
|
54
|
+
_rigidbody.velocity = Vector2.zero;//落下速度を一度リセットする
|
55
|
+
_rigidbody.AddForce(new Vector2(0, Jump));
|
56
|
+
position.y = GetBottom();//押し戻す
|
57
|
+
}
|
58
|
+
//座標を反映する
|
59
|
+
transform.position = position;
|
60
|
+
}
|
61
|
+
//画面上を習得する
|
62
|
+
float GetTop()
|
63
|
+
{
|
64
|
+
Vector2 max = Camera.main.ViewportToWorldPoint(Vector2.one);
|
65
|
+
return max.y;
|
66
|
+
}
|
67
|
+
//画面下を習得
|
68
|
+
float GetBottom()
|
69
|
+
{
|
70
|
+
//座標獲得
|
71
|
+
Vector2 min = Camera.main.ViewportToWorldPoint(Vector2.zero);
|
72
|
+
return min.y;
|
73
|
+
}
|
74
|
+
//衝突判定
|
75
|
+
private void OnTriggerEnter2D(Collider2D collision)
|
76
|
+
{
|
7
77
|
//衝突して消滅
|
8
78
|
Destroy(gameObject);
|
9
79
|
//ゲームオーバーを通知
|
10
|
-
|
80
|
+
_sc.StartGameOver();
|
11
81
|
}
|
82
|
+
|
83
|
+
}
|
12
84
|
```
|
13
85
|
コード(スコア管理の部分)
|
14
86
|
```ublic class Score : MonoBehaviour
|