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
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class sniper : MonoBehaviour
{
public float damage = 10f;
public float range = 100f;
public float fireRate = 15;
public float impactForce = 30f;
public int maxAmmo = 10;
private int currentAmmo;
public float reloadTime = 1f;
private bool isReloading = false;
public Camera fpsCam;
public ParticleSystem MuzzleFlash;
public GameObject impactEffect;
private float nextTimeToFire = 0f;
public Animator animator;
public Text ammoText;
void Start()
{
currentAmmo = maxAmmo;
}
void OnEnable()
{
isReloading = false;
animator.SetBool("reloading", false);
}
// Update is called once per frame
void Update()
{
if (isReloading)
return;
if (currentAmmo <= 0)
{
StartCoroutine(Reload());
return;
}
if (Input.GetButton("Fire1") && Time.time >= nextTimeToFire)
{
nextTimeToFire = Time.time + 1f / fireRate;
Shoot();
}
}
IEnumerator Reload()
{
isReloading = true;
Debug.Log("Reloading...");
animator.SetBool("reloading", true);
yield return new WaitForSeconds(reloadTime -.25f);
animator.SetBool("reloading", false);
yield return new WaitForSeconds(.25f);
currentAmmo = maxAmmo;
isReloading = false;
}
void Shoot()
{
MuzzleFlash.Play();
currentAmmo--;
RaycastHit hit;
if (Physics.Raycast(fpsCam.transform.position, fpsCam.transform.forward, out hit, range))
{
Debug.Log(hit.transform.name);
Target target = hit.transform.GetComponent<Target>();
if (target != null)
{
target.TakeDamage(damage);
}
if (hit.rigidbody != null)
{
hit.rigidbody.AddForce(-hit.normal * impactForce);
}
GameObject impactGO = Instantiate(impactEffect, hit.point, Quaternion.LookRotation(hit.normal));
Destroy(impactGO, 2f);
ammoText = currentAmmo;
}
}
}