I need help with my code
I’m trying to set up a rate of fire for my gun
4450606–408013–m4Gun.cs (2.02 KB)
I need help with my code
I’m trying to set up a rate of fire for my gun
4450606–408013–m4Gun.cs (2.02 KB)
Please post your script here using code tags. But based on the error, you have an variable that is declared as an int and you seem to be trying to access time off of it.
int aVariable = 1;
aVariable.time; //This would be throwing an error.
What class should i be using??
using UnityEngine;
using System.Collections;
public class m4Gun : MonoBehaviour
{
public float damage = 20f;
public float range = 100f;
public float impactforce = 30f;
public int maxAmmo = 20;
public int currentAmmo;
public float reloadTime = 5f;
public ParticleSystem m4muzzleFlash;
private bool isReloading = false;
private float nextFire;
public int timer;
public Camera fpsCam;
public Animator animator;
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 > nextFire);
{
nextFire = Time.time + 0.25f;
++timer;
Shoots();
}
}
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 Shoots ()
{
m4muzzleFlash.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);
}
}
}
}
[/QUOTE]
I think thats right
This script looks fine. Can you show us a screenshot of the actual error in the console? You might be accidentally omitting some vital information. Do you have a script in your project called ‘Time’ per chance?
Apparently you fixed it already, do you get any additional errors?
No Sorry i havent found a fix, I saying that i think ive posted my code right, sorry for the confusion
Im still getting the same original error
But the script you’ve posted has a fix for that error.
The script in your original post (the uploaded .cs file) contains this:
nextFire = timer.time + 0.25f;
“timer” being an integer, which is declared at the top of your class.
However, the code that you’ve added later does not contain the error, and uses the “Time” type correctly:
nextFire = Time.time + 0.25f;
If you applied the change to your script, the error about the int not having a “time” member should have disappeared.
Thankyou, I just checked and it was working