ボタンを押した際に弾を発射するプログラムを作り、Unity上で特に問題がなかったのでビルドし試してみましたが、なぜか数秒立ったのちに弾が発射されなくなりました。
下記にあるものがそのコードです
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class rightshooting : MonoBehaviour
{
public GameObject bullet; public Transform muzzle; public float speed = 10000; public float shotInterval;
void Update()
{
if (Input.GetKey(KeyCode.V))
{
//shoot();
shotInterval += 1;
if (shotInterval % 3 == 0) { GameObject bullets = Instantiate(bullet) as GameObject; Vector3 force; force = this.gameObject.transform.forward * speed; bullets.GetComponent<Rigidbody>().AddForce(force); bullets.transform.position = muzzle.position; StartCoroutine("sleep"); Destroy(bullet, 3.0f); } } }
ビルドしたときにエラーが表示されるようにDebuggingにチェックを入れてみたところNullReferenceExceptionがGameObject bullets = Instantiate(bullet) as GameObject;で起きているらしいのですが、呼び出しているオブジェクトを確認してもエラーが出るようなことが見当たりませんでした。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2020/01/30 07:47
2020/01/30 13:46