C
C#2y ago
Blue

❔ 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
pip
pip2y ago
you should paste your code and ask a specific question
Blue
BlueOP2y ago
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() { } }
pip
pip2y ago
what error are you getting
Blue
BlueOP2y ago
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
pip
pip2y ago
private void Start() { PlayerShoot.shootInput += Shoot; PlayerShoot.reloadInput += StartReload; } oh nvm you declared these static
Blue
BlueOP2y ago
this is what i had before i watched the video and it didnt work then either
pip
pip2y ago
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
Blue
BlueOP2y ago
just make them a public action?
pip
pip2y ago
and pass this instance of PlayerShoot into your other class
Blue
BlueOP2y ago
idk what you mean by this bit
pip
pip2y ago
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
Blue
BlueOP2y ago
the player shoots just fine though im just confused onto why the realod isnt working
pip
pip2y ago
oh ok where are you setting "gunData.reloading" to true? that's a requirement to start the coroutine
Blue
BlueOP2y ago
private IEnumerator Reload() { gunData.reloading = true; yield return new WaitForSeconds(gunData.reloadTime); gunData.currentAmmo = gunData.magSize; gunData.reloading = false; }
pip
pip2y ago
right so what's wrong with this look back to where you start the coroutine and tell me why it isn't starting
Blue
BlueOP2y ago
public void StartReload() { if (gunData.reloading) { StartCoroutine(Reload()); } } just before the IEnumerator
pip
pip2y ago
tell me specifically why it isn't starting it's simpler than you think
Blue
BlueOP2y ago
is it cause its a public void? honestly if i knew i wouldnt be here or is there a spelling mistake
pip
pip2y ago
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
Blue
BlueOP2y ago
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?
Blue
BlueOP2y ago
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...
Blue
BlueOP2y ago
8:10 you can see that part of the code in the video and he shows it working
Buddy
Buddy2y ago
$code
MODiX
MODiX2y ago
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/
Blue
BlueOP2y ago
BlazeBin - rsrmdjsqzuke
A tool for sharing your source code with the world!
Blue
BlueOP2y ago
im not sure why its not letting me reload
Accord
Accord2y ago
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.
Want results from more Discord servers?
Add your server