質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Unity3D

Unity3Dは、ゲームや対話式の3Dアプリケーション、トレーニングシュミレーション、そして医学的・建築学的な技術を可視化する、商業用の開発プラットフォームです。

Q&A

解決済

1回答

787閲覧

的あてゲームのポイント追加

Aya7

総合スコア14

Unity3D

Unity3Dは、ゲームや対話式の3Dアプリケーション、トレーニングシュミレーション、そして医学的・建築学的な技術を可視化する、商業用の開発プラットフォームです。

0グッド

0クリップ

投稿2021/07/25 07:49

前提・実現したいこと

3Dのシューティングゲームを作ろうとしています。ターゲットを壊してスコアーを追加する、そしてそのターゲットごとのポイントを追加するというものを作ろうと思っています。

もしこの問題を解決する方法、また別のやり方があれば教えていただけるとありがたいです。
プログラム初心者です。コードが汚いところばかりで申し訳ありません。

発生している問題・エラーメッセージ

そこで配列にしたらうまくいくのではないかと思い試したのですか、このようなエラーが出てしまいました。エラーメッセージを調べましたがよく理解できませんでした。
配列にしようとした場所(Scoreのスクリプトのpublic Target target;)

重大度レベル コード 説明 プロジェクト ファイル 行 抑制状態 エラー CS0650 不適切な配列の宣言子: マネージ配列を宣言するには、次元指定子を変数の識別子の前に指定します。固定サイズ バッファー フィールドを宣言するには、フィールド型の前に fixed キーワードを使用します。 Assembly-CSharp C:\Users\caram\OneDrive\デスクトップ\3D1\Assets\script\Score.cs 10 アクティブ

Score

using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class Score : MonoBehaviour { public static int ScorePoint; //得点の変数 public Text ScoreText; //得点の文字の変数 public Target target; int point; // Start is called before the first frame update void Start() { ScorePoint = 0; //得点を0にする DontDestroyOnLoad(this); ScoreText.text = "Score" + ScorePoint.ToString(); //ScoreTextの文字をScore:Scoreの値にする point = target.Point; } public void score() { ScoreText.text = "Score" + ScorePoint.ToString(); //ScoreTextの文字をScore:Scoreの値にする ScorePoint += point; } }

Target

using System.Collections; using System.Collections.Generic; using UnityEngine; public class Target : MonoBehaviour { Vector3 startPosition, targetPosition; private Vector3 velocity = Vector3.zero; public float time = 1F; public float X = 0; public float Y = 0; public float Z = 0; GameObject Gun; public GameObject target; public float DestroyTimer = 0f; public float UpTimer = 0f; public int HP = 1; public bool Up = false; public bool Destruction = false; public GameObject score; public int Point = 100; void Start() {//二点間の距離を代入(スピード調整に使う) Gun = GameObject.Find("GunController"); Gun.GetComponent<FirstPersonGunController>(); targetPosition = new Vector3(X, Y, Z); StartCoroutine("CubeCount"); score = GameObject.Find("score"); } // Update is called once per frame public void HIT() { HP -= 1; if (HP == 0) { Destroy(target.gameObject); score.GetComponent<Score>().score(); } } void Update() { if (Up) { transform.position = Vector3.SmoothDamp(transform.position, targetPosition, ref velocity, time); } if(Destruction) { Destroy(target.gameObject); } } IEnumerator CubeCount() { yield return new WaitForSeconds(UpTimer); Up = true; yield return new WaitForSeconds(DestroyTimer); Destruction = true; } }

###player

using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using KszUtil; public class FirstPersonGunController : MonoBehaviour { public enum ShootMode { AUTO, SEMIAUTO } public bool shootEnabled = true; [SerializeField] ShootMode shootMode = ShootMode.AUTO; [SerializeField] int maxAmmo = 100; [SerializeField] int maxSupplyValue = 100; [SerializeField] float shootInterval = 0.1f; [SerializeField] float shootRange = 50; [SerializeField] float supplyInterval = 0.1f; [SerializeField] Vector3 muzzleFlashScale; [SerializeField] GameObject muzzleFlashPrefab; [SerializeField] GameObject hitEffectPrefab; [SerializeField] Image ammoGauge; [SerializeField] Text ammoText; [SerializeField] Image supplyGauge; [SerializeField] private SmoothTransform aHandGun; [SerializeField] private SmoothTransform bHandGun; bool shooting = false; bool supplying = false; int ammo = 0; int supplyValue = 0; GameObject muzzleFlash; GameObject hitEffect; public int Ammo { set { ammo = Mathf.Clamp(value, 0, maxAmmo); //UIの表示を操作 //テキスト ammoText.text = ammo.ToString("D3"); //ゲージ float scaleX = (float)ammo / maxAmmo; ammoGauge.rectTransform.localScale = new Vector3(scaleX, 1, 1); } get { return ammo; } } public int SupplyValue { set { supplyValue = Mathf.Clamp(value, 0, maxSupplyValue); if (SupplyValue >= maxSupplyValue) { Ammo = maxAmmo; supplyValue = 0; } float scaleX = (float)supplyValue / maxSupplyValue; supplyGauge.rectTransform.localScale = new Vector3(scaleX, 1, 1); } get { return supplyValue; } } void Start() { InitGun(); } void Update() { if (shootEnabled & ammo > 0 & GetInput()) { StartCoroutine(ShootTimer()); } if (shootEnabled) { StartCoroutine(SupplyTimer()); } ADS(); } public void InitGun() { Ammo = maxAmmo; SupplyValue = 0; } bool GetInput() { switch (shootMode) { case ShootMode.AUTO: return Input.GetMouseButton(0); case ShootMode.SEMIAUTO: return Input.GetMouseButtonDown(0); } return false; } IEnumerator ShootTimer() { if (!shooting) { shooting = true; //マズルフラッシュON if (muzzleFlashPrefab != null) { if (muzzleFlash != null) { muzzleFlash.SetActive(true); } else { muzzleFlash = Instantiate(muzzleFlashPrefab, transform.position, transform.rotation); muzzleFlash.transform.SetParent(gameObject.transform); muzzleFlash.transform.localScale = muzzleFlashScale; } } Shoot(); yield return new WaitForSeconds(shootInterval); //マズルフラッシュOFF if (muzzleFlash != null) { muzzleFlash.SetActive(false); } //ヒットエフェクトOFF if (hitEffect != null) { if (hitEffect.activeSelf) { hitEffect.SetActive(false); } } shooting = false; } else { yield return null; } } public void Shoot() { Ray ray = new Ray(transform.position, transform.forward); RaycastHit hit; //レイを飛ばして、ヒットしたオブジェクトの情報を得る if (Physics.Raycast(ray, out hit, shootRange)) { //ヒットエフェクトON if (hitEffectPrefab != null) { if (hitEffect != null) { hitEffect.transform.position = hit.point; hitEffect.transform.rotation = Quaternion.FromToRotation(Vector3.forward, hit.normal); hitEffect.SetActive(true); } else { hitEffect = Instantiate(hitEffectPrefab, hit.point, Quaternion.identity); } } string tagName = hit.collider.gameObject.tag; if (tagName == "Enemy") { var _target = hit.collider.gameObject.GetComponent<Target>(); if (_target != null) _target.HIT(); } } Ammo--; } IEnumerator SupplyTimer() { if (!supplying) { supplying = true; SupplyValue++; yield return new WaitForSeconds(supplyInterval); supplying = false; } } void ADS() { if (Input.GetMouseButtonDown(1)) { //transform.positionではなく、TargetPositionに var aPos = aHandGun.TargetPosition; var bPos = bHandGun.TargetPosition; bHandGun.TargetPosition = aPos; aHandGun.TargetPosition = bPos; } else if(Input.GetMouseButtonUp(1)) { var aPos = aHandGun.TargetPosition; var bPos = bHandGun.TargetPosition; bHandGun.TargetPosition = aPos; aHandGun.TargetPosition = bPos; } } }

Target…何回か充てるとこをれるようになっています。
Score …Targetから値を読み込みスコアーを加算しようと思いました。

補足情報(FW/ツールのバージョンなど)

Unity 2020.3.4f1

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

GameObject.Find("score").score();

targetクラスのhpが無くなった部分、そこでスコアを加算すると思われるのですがtarget本体がpoint(点数?)を持っていますので、
scoreというゲームオブジェクトが必ず存在している、という体でtarget側からそこに引数として加算したい値を渡してあげるのが分かりやすくて良い気がします。

Scoreクラスは
public void Score(int point)
に、

Targetクラスの上記部分を
GameObject.Find("score").score(point);

だけで出来るような気がします。細かくは見てませんので何となくです。

Scoreクラスがtargetを知る必要は無いかな?と思います。
なんでもかんでも参照を渡してしまうと循環参照という状態になってしまいます。これが良くないって事はググってください。初心者という事なので、分からなかったら今は無視しても大丈夫です。後々、知識を得た後に調べればいい事です。

質問の内容(正直???って思う感じで理解できてないですが)で配列を使わないといけない、使った方が便利、と感じる部分は無い気がします。

cs0650はどこ?は見つけられなかったのですが、ただのsyntax errorで文法間違いではないでしょうか。配列を宣言する書き方が間違っているだけじゃないかと。

投稿2021/07/27 07:53

退会済みユーザー

退会済みユーザー

総合スコア0

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

Aya7

2021/07/28 01:08

質問がわかりにくくて申し訳ありません。 参照しすぎることはよくないのですね。 よく理解しましたありがとうございます。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問