Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question Help needed fps remaining bullet count HUD

Discussion in 'Scripting' started by FirstBB8Droid, May 31, 2020.

  1. FirstBB8Droid

    FirstBB8Droid

    Joined:
    May 17, 2020
    Posts:
    39
    I'm trying to add a bullet counter to my game to display your remaining bullets and I just need a little bit of help, I'm getting the error
    Assets\Gun Scripts\sniper.cs(88,24): error CS0029: Cannot implicitly convert type 'int' to 'UnityEngine.UI.Text'
    Here's the code
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.UI;
    4. public class sniper : MonoBehaviour
    5. {
    6.  
    7.     public float damage = 10f;
    8.     public float range = 100f;
    9.     public float fireRate = 15;
    10.     public float impactForce = 30f;
    11.  
    12.     public int maxAmmo = 10;
    13.     private int currentAmmo;
    14.     public float reloadTime = 1f;
    15.     private bool isReloading = false;
    16.  
    17.     public Camera fpsCam;
    18.     public ParticleSystem MuzzleFlash;
    19.     public GameObject impactEffect;
    20.  
    21.     private float nextTimeToFire = 0f;
    22.  
    23.     public Animator animator;
    24.  
    25.     public Text ammoText;
    26.     void Start()
    27.     {
    28.         currentAmmo = maxAmmo;
    29.     }
    30.     void OnEnable()
    31.     {
    32.         isReloading = false;
    33.         animator.SetBool("reloading", false);
    34.     }
    35.     // Update is called once per frame
    36.     void Update()
    37.     {
    38.         if (isReloading)
    39.             return;
    40.  
    41.         if (currentAmmo <= 0)
    42.         {
    43.             StartCoroutine(Reload());
    44.             return;
    45.         }
    46.         if (Input.GetButton("Fire1") && Time.time >= nextTimeToFire)
    47.         {
    48.             nextTimeToFire = Time.time + 1f / fireRate;
    49.             Shoot();
    50.         }
    51.      
    52.     }
    53.     IEnumerator Reload()
    54.     {
    55.         isReloading = true;
    56.         Debug.Log("Reloading...");
    57.         animator.SetBool("reloading", true);
    58.         yield return new WaitForSeconds(reloadTime -.25f);
    59.         animator.SetBool("reloading", false);
    60.         yield return new WaitForSeconds(.25f);
    61.         currentAmmo = maxAmmo;
    62.         isReloading = false;
    63.     }
    64.     void Shoot()
    65.     {
    66.         MuzzleFlash.Play();
    67.  
    68.         currentAmmo--;
    69.  
    70.         RaycastHit hit;
    71.         if (Physics.Raycast(fpsCam.transform.position, fpsCam.transform.forward, out hit, range))
    72.         {
    73.             Debug.Log(hit.transform.name);
    74.  
    75.             Target target = hit.transform.GetComponent<Target>();
    76.             if (target != null)
    77.             {
    78.                 target.TakeDamage(damage);
    79.             }
    80.  
    81.             if (hit.rigidbody != null)
    82.             {
    83.                 hit.rigidbody.AddForce(-hit.normal * impactForce);
    84.             }
    85.  
    86.             GameObject impactGO = Instantiate(impactEffect, hit.point, Quaternion.LookRotation(hit.normal));
    87.             Destroy(impactGO, 2f);
    88.             ammoText = currentAmmo;
    89.         }
    90.     }
    91. }
    92.  
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,893
    Code (CSharp):
    1. ammoText.text = currentAmmo.ToString();
     
    FirstBB8Droid and Dextozz like this.
  3. FirstBB8Droid

    FirstBB8Droid

    Joined:
    May 17, 2020
    Posts:
    39
    Thanks