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

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

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

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

UI

UIはUser Interfaceの略であり、人間がコンピュータとやりとりをするためのシステムです。

Unity3D

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

Unity

Unityは、Unity Technologiesが開発・販売している、IDEを内蔵するゲームエンジンです。主にC#を用いたプログラミングでコンテンツの開発が可能です。

Q&A

解決済

2回答

1858閲覧

UnityでUIを追加したい

退会済みユーザー

退会済みユーザー

総合スコア0

C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

UI

UIはUser Interfaceの略であり、人間がコンピュータとやりとりをするためのシステムです。

Unity3D

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

Unity

Unityは、Unity Technologiesが開発・販売している、IDEを内蔵するゲームエンジンです。主にC#を用いたプログラミングでコンテンツの開発が可能です。

0グッド

0クリップ

投稿2019/12/13 04:00

編集2020/01/01 05:23

https://unity.moon-bear.com/zombie-slayer/gauge-and-score/

実現したいことは上記サイトのものをそのまままねることです。

このサイトをベースに進めていて、UIを作るところまで来たのですがここのコードは間違っているとは思えないのですが、変数の中身が指定されていないのようなエラーがでました。
実行して落ちなかった時には補足のところに書いてあるものしかエラーが出ませんでした。

###今の画面
現在の画面です

発生している問題・警告

Assets\UnityChan\SplashScreen\Scripts\SplashScreen.cs(11,27): warning CS0618: 'Application.loadedLevel' is obsolete: 'Use SceneManager to determine what scenes have been loaded' Assets\UnityChan\SplashScreen\Scripts\SplashScreen.cs(11,4): warning CS0618: 'Application.LoadLevel(int)' is obsolete: 'Use SceneManager.LoadScene' Assets\UnityChan\Scripts\IdleChanger.cs(59,9): warning CS0618: 'AnimatorStateInfo.nameHash' is obsolete: 'AnimatorStateInfo.nameHash has been deprecated. Use AnimatorStateInfo.fullPathHash instead.' Assets\UnityChan\Scripts\IdleChanger.cs(59,35): warning CS0618: 'AnimatorStateInfo.nameHash' is obsolete: 'AnimatorStateInfo.nameHash has been deprecated. Use AnimatorStateInfo.fullPathHash instead.' Assets\UnityChan\Scripts\IdleChanger.cs(69,9): warning CS0618: 'AnimatorStateInfo.nameHash' is obsolete: 'AnimatorStateInfo.nameHash has been deprecated. Use AnimatorStateInfo.fullPathHash instead.' Assets\UnityChan\Scripts\IdleChanger.cs(69,35): warning CS0618: 'AnimatorStateInfo.nameHash' is obsolete: 'AnimatorStateInfo.nameHash has been deprecated. Use AnimatorStateInfo.fullPathHash instead.' Assets\UnityChan\Scripts\UnityChanControlScriptWithRgidBody.cs(96,9): warning CS0618: 'AnimatorStateInfo.nameHash' is obsolete: 'AnimatorStateInfo.nameHash has been deprecated. Use AnimatorStateInfo.fullPathHash instead.' Assets\UnityChan\Scripts\UnityChanControlScriptWithRgidBody.cs(116,8): warning CS0618: 'AnimatorStateInfo.nameHash' is obsolete: 'AnimatorStateInfo.nameHash has been deprecated. Use AnimatorStateInfo.fullPathHash instead.' Assets\UnityChan\Scripts\UnityChanControlScriptWithRgidBody.cs(124,12): warning CS0618: 'AnimatorStateInfo.nameHash' is obsolete: 'AnimatorStateInfo.nameHash has been deprecated. Use AnimatorStateInfo.fullPathHash instead.' Assets\UnityChan\Scripts\UnityChanControlScriptWithRgidBody.cs(160,12): warning CS0618: 'AnimatorStateInfo.nameHash' is obsolete: 'AnimatorStateInfo.nameHash has been deprecated. Use AnimatorStateInfo.fullPathHash instead.' Assets\UnityChan\Scripts\UnityChanControlScriptWithRgidBody.cs(172,12): warning CS0618: 'AnimatorStateInfo.nameHash' is obsolete: 'AnimatorStateInfo.nameHash has been deprecated. Use AnimatorStateInfo.fullPathHash instead.' Assets\Scripts\FirstPersonGunController.cs(28,13): warning CS0649: Field 'FirstPersonGunController.muzzleFlashScale' is never assigned to, and will always have its default value Assets\Scripts\FirstPersonGunController.cs(32,16): warning CS0649: Field 'FirstPersonGunController.hitEffectPrefab' is never assigned to, and will always have its default value null Assets\Scripts\FirstPersonGunController.cs(36,10): warning CS0649: Field 'FirstPersonGunController.ammoText' is never assigned to, and will always have its default value null Assets\Scripts\FirstPersonGunController.cs(38,11): warning CS0649: Field 'FirstPersonGunController.supplyGuage' is never assigned to, and will always have its default value null Assets\Scripts\FirstPersonGunController.cs(34,11): warning CS0649: Field 'FirstPersonGunController.ammoGauge' is never assigned to, and will always have its default value null Assets\Scripts\FirstPersonGunController.cs(41,15): warning CS0649: Field 'FirstPersonGunController.FireSound' is never assigned to, and will always have its default value null Assets\Scripts\FirstPersonGunController.cs(30,16): warning CS0649: Field 'FirstPersonGunController.muzzleFlashPrefab' is never assigned to, and will always have its default value null Assets\Scripts\FirstPersonGunController.cs(51,19): warning CS0414: The field 'FirstPersonGunController.reloadTime' is assigned but its value is never used Assets\Scripts\FirstPersonGunController.cs(19,9): warning CS0414: The field 'FirstPersonGunController.damage' is assigned but its value is never used

文字数の都合で勝手に追加したものは消しました。

該当のソースコード

C#

1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4using UnityEngine.UI; 5 6public class FirstPersonGunController : MonoBehaviour 7{ 8 public enum ShootMode { AUTO, SEMIAUTO } 9 10 public bool shootEnabled = true; 11 12 public ShootMode shootMode = ShootMode.AUTO; 13 public int maxAmmo = 30; 14 public int damage = 1; 15 public float shootInterval = 0.1f; 16 public float shootRange = 50; 17 [SerializeField] 18 int maxSupplyValue = 30; 19 [SerializeField] 20 float supplyInterval = 0.1f; 21 22 public Vector3 muzzleFlashScale; 23 public GameObject muzzleFlashPrefab; 24 public GameObject hitEffectPrefab; 25 [SerializeField] 26 Image ammoGauge; 27 [SerializeField] 28 Text ammoText; 29 [SerializeField] 30 Image supplyGuage; 31 32 bool shooting = false; 33 bool supplying = false; 34 int ammo = 0; 35 int supplyValue = 0; 36 37 GameObject muzzleFlash; 38 GameObject hitEffect; 39 40 void Start() 41 { 42 InitGun(); 43 } 44 45 void Update() 46 { 47 if (shootEnabled & ammo > 0 & GetInput()) 48 { 49 StartCoroutine(ShootTimer()); 50 } 51 52 if (shootEnabled) 53 { 54 StartCoroutine(SupplyTimer()); 55 } 56 } 57 58 public int Ammo 59 { 60 set 61 { 62 ammo = Mathf.Clamp(value, 0, maxAmmo); 63 64 ammoText.text = ammo.ToString("D3"); 65 66 float scaleX = (float)ammo / maxAmmo; 67 ammoGauge.rectTransform.localScale = new Vector3(scaleX, 1, 1); 68 } 69 get { return ammo; } 70 } 71 72 public int SupplyValue 73 { 74 set 75 { 76 SupplyValue = Mathf.Clamp(value, 0, maxSupplyValue); 77 78 if (SupplyValue >= maxSupplyValue) 79 { 80 Ammo = maxAmmo; 81 supplyValue = 0; 82 } 83 84 float scaleX = (float)supplyValue / maxSupplyValue; 85 supplyGuage.rectTransform.localScale = new Vector3(scaleX, 1, 1); 86 } 87 get { return supplyValue; } 88 } 89 90 void InitGun() { Ammo = maxAmmo; } 91 92 bool GetInput() 93 { 94 switch (shootMode) 95 { 96 case ShootMode.AUTO: 97 return Input.GetMouseButton(0); 98 99 case ShootMode.SEMIAUTO: 100 return Input.GetMouseButtonDown(0); 101 } 102 return false; 103 } 104 105 IEnumerator ShootTimer() 106 { 107 if (!shooting) 108 { 109 shooting = true; 110 111 if (muzzleFlashPrefab != null) 112 { 113 if (muzzleFlash != null) 114 { 115 muzzleFlash.SetActive(true); 116 } 117 else 118 { 119 muzzleFlash = Instantiate(muzzleFlashPrefab, transform.position, transform.rotation); 120 muzzleFlash.transform.SetParent(gameObject.transform); 121 muzzleFlash.transform.localScale = muzzleFlashScale; 122 } 123 } 124 Shoot(); 125 126 yield return new WaitForSeconds(shootInterval / 2); 127 128 if (muzzleFlash != null) 129 { 130 muzzleFlash.SetActive(false); 131 } 132 133 if (hitEffect != null) 134 { 135 if (hitEffect.activeSelf) 136 { 137 hitEffect.SetActive(false); 138 } 139 } 140 yield return new WaitForSeconds(shootInterval / 2); 141 142 shooting = false; 143 } 144 else 145 { 146 yield return null; 147 } 148 } 149 150 IEnumerator SupplyTimer() 151 { 152 if (!supplying) 153 { 154 supplying = true; 155 SupplyValue++; 156 yield return new WaitForSeconds(supplyInterval); 157 supplying = false; 158 } 159 } 160 161 void Shoot() 162 { 163 Ray ray = new Ray(transform.position, transform.forward); 164 RaycastHit hit; 165 166 if (Physics.Raycast(ray, out hit, shootRange)) 167 { 168 if (hitEffectPrefab != null) 169 { 170 if (hitEffect != null) 171 { 172 hitEffect.transform.position = hit.point; 173 hitEffect.transform.rotation = Quaternion.FromToRotation(Vector3.forward, hit.normal); 174 hitEffect.SetActive(true); 175 } 176 else 177 { 178 hitEffect = Instantiate(hitEffectPrefab, hit.point, Quaternion.identity); 179 } 180 } 181 } 182 Ammo--; 183 } 184}

試したこと

SupplyValue = Mathf.Clamp(value, 0, maxSupplyValue); と
ammoText.text = ammo.ToString("D3"); の部分でも吐いていました。valueを何かに変える、D3を変えるのでしょうが全くわかりません。

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

Unity 2019.2.11.f1を使っています。
Visual stadio 2019 comunityを使ってます。最新アプデ済みです。
Unity-chanはAssetだけ実装(?)はしていないです。マテリアルだけ使おうかなとか思ってました。
MainCameraのC#ファイルのところにあるAmmoGauge、AmmoText、SupplyGaugeはなにも指定していません。指定した場合Unityが落ちます。
現状エラー文を取り出そうとしても、上記のコードでエディタを再生するとUnityが応答なしになってしまい、動かないのでコードだけでわかることがあればという状況です。。

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

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

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

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

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

Y0241-N

2019/12/13 04:21

エラーメッセージのところに貼られているエラー内容を読みましたが、CS0618エラーは使用されているコードの一部分('Application.loadedLevel' )が古いので置き換えてくださいというメッセージで、 CS0649は指定された変数はコード内部で値が割り当てられず常にnullの値となっているという警告、これは値を割り当てていても出ることがあります、何故かは知りません。 CS0414はこの変数は宣言されているけど、コード内部で使用されていませんよ。というメッセージ。 つまりこれらはすべて警告なので、実際に発生しているエラーが何なのか分かりません。 現状どういった状態なんでしょうか?エディタを再生すると落ちるという事ですか?
退会済みユーザー

退会済みユーザー

2019/12/13 08:55

書く前は再生すると止まって5秒ぐらいで落ちたのですが、今やったら止まったままになってしまいました。またスクリプトを適応せずに再生しても止まってしまいました。警告なのはわかってましたが参考になればと思ったのですが、やはりそんなにいらないですよね、、 ほかのものでエディタを再生してみるとできたので、Unityの問題ではなさそうです
guest

回答2

0

ベストアンサー

UIの一部をプレハブにするのを忘れていたためできませんでした。
ですがそれだけでUnityが動かなくなるのはなぜなのでしょうか?

投稿2020/01/01 09:36

退会済みユーザー

退会済みユーザー

総合スコア0

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

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

0

とりあえず提示されているのはエラーではなく警告です。そのままでも動きます。
実行環境の割にソースコードが古いのが問題かと。

CS0618 AAA is obsolete: BBBとなっているもの
→「AAAは古くなっているから非推奨。代わりにBBBを使え」(AAAでググれば対処方法が大体出ます)

CS0649 → 以下参照。
[SerializeField] 属性をつけていると、CS0649警告が出るようになった。消したい。 - Qiita

他のメッセージについてもググれば大体対処が出て来ると思うので、まずは自分で確認してください。

投稿2019/12/13 04:16

sakura_hana

総合スコア11427

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問