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が応答なしになってしまい、動かないのでコードだけでわかることがあればという状況です。。
回答2件
あなたの回答
tips
プレビュー