❔ having issues on a reloading script on unity to reload weapons
the code is used in a youtube video and i have pretty much copied to to get it to work but it doesnt seem to want to work
27 Replies
you should paste your code and ask a specific question
using System;
using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;
public class Gun : MonoBehaviour
{
[Header("References")]
[SerializeField] private GunData gunData;
[SerializeField] private Transform muzzle;
float timeSinceLastShot;
private void Start()
{
PlayerShoot.shootInput += Shoot;
PlayerShoot.reloadInput += StartReload;
}
public void StartReload()
{
if (gunData.reloading) {
StartCoroutine(Reload());
}
}
private IEnumerator Reload()
{
gunData.reloading = true;
yield return new WaitForSeconds(gunData.reloadTime);
gunData.currentAmmo = gunData.magSize;
gunData.reloading = false;
}
private bool CanShoot() => !gunData.reloading && timeSinceLastShot > 1f / (gunData.fireRate / 60f);
public void Shoot()
{
if (gunData.currentAmmo > 0)
{
if (CanShoot())
{
if (Physics.Raycast(muzzle.position, transform.forward, out RaycastHit hitInfo, gunData.maxDistance))
{
Debug.Log(hitInfo.transform.name);
}
gunData.currentAmmo--;
timeSinceLastShot = 0;
OnGunShot();
}
}
}
private void Update()
{
timeSinceLastShot += Time.deltaTime;
Debug.DrawRay(muzzle.position, muzzle.forward);
}
private void OnGunShot()
{
}
}
what error are you getting
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerShoot : MonoBehaviour
{
public static Action shootInput;
public static Action reloadInput;
[SerializeField] private KeyCode reloadKey = KeyCode.R;
private void Update()
{
if (Input.GetMouseButton(0))
shootInput?.Invoke();
if (Input.GetKeyDown(reloadKey))
reloadInput?.Invoke();
}
}
this is the Reloading script
but im not having any errors its just not working when i use R to reload
on the video the guy does this and tests it straight away and it shows in the console on unity that he is reloading
but for me it doesnt show
private void Start()
{
PlayerShoot.shootInput += Shoot;
PlayerShoot.reloadInput += StartReload;
}
oh nvm you declared these static
this is what i had before i watched the video and it didnt work then either
well iirc Unity has a weird relationship with "static"
they use their own version of C# which excludes constructors at a minimum
so what i think you should do is not make those static
just make them a public action?
and pass this instance of PlayerShoot into your other class
idk what you mean by this bit
So in Gun make "public PlayerShoot playerShoot;" then assign it to your gameobject in editor
then just change what you ahve on Start to use playerShoot instead of PlayerShoot
the player shoots just fine though im just confused onto why the realod isnt working
oh ok
where are you setting "gunData.reloading" to true?
that's a requirement to start the coroutine
private IEnumerator Reload()
{
gunData.reloading = true;
yield return new WaitForSeconds(gunData.reloadTime);
gunData.currentAmmo = gunData.magSize;
gunData.reloading = false;
}
right
so what's wrong with this
look back to where you start the coroutine
and tell me why it isn't starting
public void StartReload()
{
if (gunData.reloading) {
StartCoroutine(Reload());
}
}
just before the IEnumerator
tell me specifically why it isn't starting
it's simpler than you think
is it cause its a public void?
honestly if i knew i wouldnt be here
or is there a spelling mistake
you are doing a boolean check
on something that isn't set to true anywhere
the only time reloading is set to true is AFTER the true check
so you have to fix that
how do i fix that
private IEnumerator Reload()
{
gunData.reloading = false;
yield return new WaitForSeconds(gunData.reloadTime);
gunData.currentAmmo = gunData.magSize;
gunData.reloading = true;
}
like that?
Plai
YouTube
Unity Basic Weapon System Tutorial
I present to you my longest video yet. Sorry for all the mistakes in the video, I usually don't make such long videos.
In this video, I teach you how to create a simple weapon system. In future episodes, we'll add weapon switching, and visual/sound effects.
PROJECT LINK: https://github.com/Plai-Dev/weapon-system/
Like & Subscribe!
Timestamps...
8:10
you can see that part of the code in the video and he shows it working
$code
To post C# code type the following:
```cs
// code here
```
Get an example by typing
$codegif
in chat
If your code is too long, post it to: https://paste.mod.gg/BlazeBin - rsrmdjsqzuke
A tool for sharing your source code with the world!
im not sure why its not letting me reload
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.