C
C#루카스

❔ 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
P
pip398d ago
you should paste your code and ask a specific question
루카스398d 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() { } }
P
pip398d ago
what error are you getting
루카스398d 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
P
pip398d ago
private void Start() { PlayerShoot.shootInput += Shoot; PlayerShoot.reloadInput += StartReload; } oh nvm you declared these static
루카스398d ago
this is what i had before i watched the video and it didnt work then either
P
pip398d 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
루카스398d ago
just make them a public action?
P
pip398d ago
and pass this instance of PlayerShoot into your other class
루카스398d ago
idk what you mean by this bit
P
pip398d 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
루카스398d ago
the player shoots just fine though im just confused onto why the realod isnt working
P
pip398d ago
oh ok where are you setting "gunData.reloading" to true? that's a requirement to start the coroutine
루카스398d ago
private IEnumerator Reload() { gunData.reloading = true; yield return new WaitForSeconds(gunData.reloadTime); gunData.currentAmmo = gunData.magSize; gunData.reloading = false; }
P
pip398d ago
right so what's wrong with this look back to where you start the coroutine and tell me why it isn't starting
루카스398d ago
public void StartReload() { if (gunData.reloading) { StartCoroutine(Reload()); } } just before the IEnumerator
P
pip398d ago
tell me specifically why it isn't starting it's simpler than you think
루카스398d ago
is it cause its a public void? honestly if i knew i wouldnt be here or is there a spelling mistake
P
pip398d 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
루카스398d 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?
루카스398d 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...
루카스398d ago
8:10 you can see that part of the code in the video and he shows it working
B
Buddy398d ago
$code
M
MODiX398d 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/
루카스398d ago
BlazeBin - rsrmdjsqzuke
A tool for sharing your source code with the world!
루카스398d ago
im not sure why its not letting me reload
A
Accord397d 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
More Posts
❔ help with an exerciseI've encountered a weird problem that idk how to fix. Say we've got a string "31131123521" how do i ❔ Accessing HTTP Context at DbCommandInterceptor [.NET 7]Hi! I'm having some issues trying to access the HTTP Context at my DbCommand Interceptor class. What❔ XMLAttributeCollection -> Dictionary with LINQ?It's a confusing class. It only allows turning it into a Queryable, but I have no experience with th❔ MS SQL Reporting Server URL Being RedirectedWe have an application from a vendor that was written in C# and we recently upgraded the MS SQL data❔ dataset memory leakI found memory leak and I don't understand how to fix it Test case: ```cpp public class MemoryLeaksT❔ WebAPI .NET 7.0 - Encrypt connectionstring in appsettings.jsonguys, I'm writing some WebAPI using .NET 7.0, and I'd like to secure the connectionstring with encry❔ how to read strings correctly with System.Data.SQLitei m trying read turkish characters from database but i can't see correctly❔ Blazor server app, base url / base page to set environmentHi 🙂 I have a Blazor Server app, where I would like to use some dynamic baseurl. Say instead of my❔ C# Console Application -its only writing the same random string, how can i make it do different ones?❔ Rapid prototypingDo you feel there is such a thing as rapid prototyping or mvp development where you do things differ❔ HotChocolate with IQueryable, apply a required filter on the ef entityBasically, I'm trying to find a way to configure an `IObjectFieldDescriptor` by adding a required ar❔ Dev/prod databseHello, Me and a friend wants to create a webb-app project with a react frontend and C# backend. ❔ Book recommendation to learn c#/dotnetI've been programming in python for a little under two years. I am starting a new job in c#. Whats a❔ Use `this` inside method decoratorTried to follow https://stackoverflow.com/a/2966758 ```cs [AttributeUsage(AttributeTargets.Metho✅ How can I make VSCode auto add semicolons to statements on save?I'm coming from the world of Javascript and Typescript. There, you can just use Prettier to automati❔ Find common substring at start of every string in listI have a list of Windows path strings and I want to determine the common beginning shared by all of ❔ Code is completely skipping over an areaI'm writing a script for a game where if you don't press Spacebar within 4 seconds, the number of fa❔ Registering an extracted appx package```csharp public static void RegisterMinecraftAppxPackage(string appxPackagePath) { try { ✅ Exception Handling for NRE without modifying every call.I have a ReadData function which reads the memory of a process. If the process exits or crashes duri✅ please help idk what to do or what i didSeverity Code Description Project File Line Suppression State Error Project