unity クリック箇所にオブジェクトを出したいです。

実現したいこと

クリックした箇所にオブジェクトを生成したいと思っています。

発生している問題・分からないこと

unityで、2Dにおいてクリックした場所にオブジェクトを生成するスクリプトを作っています。しかし下記のようなエラーが出てしまいオブジェクトが生成されません。ヒエラルキーにも表示されません。

エラーメッセージ

error

1NullReferenceException: Object reference not set to an instance of an object 2start.Update () (at Assets/start.cs:32)

該当のソースコード

C#

1using System.Collections;2using System.Collections.Generic;3using UnityEngine;4using UnityEngine.SceneManagement;5 6public class start : MonoBehaviour 7{8 float mas = 0;9 Rigidbody rigid2d;10 Vector2 startPos ;11 Vector3 startPos2;12 private float speed=5;13 public GameObject Player;14 private GameObject gene;15 16 17 void Start()18 {19 this.rigid2d = GetComponent<Rigidbody>();20 this.speed = 100;21 rigid2d.constraints= RigidbodyConstraints.FreezeAll;22 23 }24 25 void Update()26 {27 28 // マウスの動きと反対方向に発射される29 if (Input.GetMouseButtonDown(0))30 {31 this.startPos = Input.mousePosition;32 Vector3 startPos2 = UnityEngine.Camera.main.ScreenToWorldPoint(Input.mousePosition);33 gene=Instantiate(Player, startPos2, Quaternion.identity);34 35 }36 37 else if (Input.GetMouseButtonUp(0))38 {39 rigid2d.constraints = RigidbodyConstraints.None;40 Vector2 endPos = Input.mousePosition;41 float dis = Vector2.Distance(startPos, endPos);42 float speed = dis * 3f;43 const float min = 30f;44 const float max = 400f;45 speed = Mathf.Clamp(speed, min, max);46 Vector2 startDirection = -1 * (endPos - startPos).normalized;47 this.rigid2d.AddForce(startDirection * speed);48 mas = 2;49 }50 51 52 // テスト用:スペースキー押下で停止53 if (Input.GetKeyDown(KeyCode.Space))54 {55 this.rigid2d.velocity *= 0;56 }57 58 59 // Spaceキーを離すと動き出す60 //if(Input.GetKeyUp(KeyCode.Space)) {61 //62 // this.speed = 100.0f;63 // this.rigid2d.AddForce(transform.up * speed);64 //}65 66 }67 68 void FixedUpdate()69 {70 71 this.rigid2d.velocity *= 0.995f;72 73 }74 75}

試したこと・調べたこと

上記の詳細・結果

オブジェクトがアクティブになっているかどうかも確認しました。

補足

特になし

コメントを投稿

0 コメント