Idk How To do this

I am currently making a fps and I cant get my way around this obstacle.
For context I am trying to play a reload animation , but only if the gun has an attachment . I tried to do this in multiple ways but it doesn’t even play a reload animation when the gun has the attachment in the current state.
I thought I’d ask for help here , since I can’t find any tutorial on the matter at hand.

This is my code :

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using static UnityEngine.GraphicsBuffer;
using TMPro;
public class WeaponScr : MonoBehaviour
{
public float damage = 10f;
public float range = 100f;
public float fireRate = 15f;
public float impactForce = 30f;
public int maxAmmo = 10;
public int currentAmmo;
public float reloadTime = 1f;
private bool isRealoading = false;
public bool isAttached = false;
public AudioSource sounds;
public AudioClip[ ] audioArray;
public AudioSource reloadSounds;
public AudioSource clip2;
public Animator animator;
public Camera fpscamera;
public ParticleSystem muzzleflash;
public GameObject impactEffect;
public GameObject ammoDisplay;
public GameObject reloadText;
private ProceduralRecoilWeapon recoilWeapon;
private float nextTimeToFire = 0f;
[Header(“Reference Points”)]
public Transform recoilPosition;
public Transform rotationPoint;
[Space(10)]
[Header(“Speed Settings”)]
public float positionalRecoilSpeed = 8f;
public float rotationalRecoilSpeed = 8f;
[Space(10)]
public float positionalReturnSpeed = 18f;
public float rotationalReturnSpeed = 38f;
[Space(10)]
[Header(“Amount Settings:”)]
public Vector3 RecoilRotation = new Vector3(10, 5, 7);
public Vector3 RecoilKickBack = new Vector3(0.015f, 0f, -0.2f);
[Space(10)]
public Vector3 RecoilRotationAim = new Vector3(10, 4, 6);
public Vector3 RecoilKickBackAim = new Vector3(0.015f, 0f, -0.2f);
[Space(10)]
Vector3 rotationalRecoil;
Vector3 positionalRecoil;
Vector3 Rot;
[Header(“State:”)]
public bool aiming;
void Start()
{
currentAmmo = maxAmmo;
sounds.clip = audioArray[Random.Range(0, audioArray.Length)];
}
private void FixedUpdate()
{
rotationalRecoil = Vector3.Lerp(rotationalRecoil, Vector3.zero, rotationalReturnSpeed * Time.deltaTime);
positionalRecoil = Vector3.Lerp(positionalRecoil, Vector3.zero, positionalReturnSpeed * Time.deltaTime);
recoilPosition.localPosition = Vector3.Slerp(recoilPosition.localPosition, positionalRecoil, positionalRecoilSpeed * Time.deltaTime);
Rot = Vector3.Slerp(Rot, rotationalRecoil, rotationalRecoilSpeed * Time.deltaTime);
rotationPoint.localRotation = Quaternion.Euler(Rot);
Attach();
}
void Update()
{
if (isRealoading)
return;
if (currentAmmo <= 0)
{
StartCoroutine(Reload());
return;
}
if (Input.GetKey(KeyCode.Mouse0) && Time.time >= nextTimeToFire)
{
nextTimeToFire = Time.time + 1f / fireRate;
Shoot();
}
ShowAmmo();
}
void OnEnable()
{
isRealoading = false;
animator.SetBool(“Reloading”, false);
}
IEnumerator Reload()
{
isRealoading = true;
Debug.Log(“Reloading…”);
animator.SetBool(“Reloading”, true);
yield return new WaitForSeconds(.5f);
reloadSounds.Play();
yield return new WaitForSeconds(1.5f);
clip2.Play();
yield return new WaitForSeconds(reloadTime - .25f);
animator.SetBool(“Reloading”, false);
yield return new WaitForSeconds(.25f);
currentAmmo = maxAmmo;
isRealoading = false;
}
void Shoot()
{
muzzleflash.Play();
sounds.PlayOneShot(sounds.clip);
currentAmmo–;
RaycastHit hit;
if (Physics.Raycast(fpscamera.transform.position, fpscamera.transform.forward, out hit, range))
{
UnityEngine.Debug.Log(hit.transform.name);
Target target = hit.transform.GetComponent();
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);
}
if (aiming)
{
rotationalRecoil += new Vector3(-RecoilRotationAim.x, Random.Range(-RecoilRotationAim.y, RecoilRotationAim.y), Random.Range(-RecoilRotationAim.z, RecoilRotationAim.z));
positionalRecoil += new Vector3(Random.Range(-RecoilKickBackAim.x, RecoilKickBackAim.x), Random.Range(-RecoilKickBackAim.y, RecoilKickBackAim.y), RecoilKickBackAim.z);
}
else
{
rotationalRecoil += new Vector3(-RecoilRotation.x, Random.Range(-RecoilRotation.y, RecoilRotation.y), Random.Range(-RecoilRotation.z, RecoilRotation.z));
rotationalRecoil += new Vector3(Random.Range(-RecoilKickBack.x, RecoilKickBack.x), Random.Range(-RecoilKickBack.y, RecoilKickBack.y), RecoilKickBack.z);
}
}
public void ShowAmmo()
{
ammoDisplay.GetComponent<TMP_Text>().text = currentAmmo + “/20”;
}
public void Attach()
{
if (Input.GetKeyDown(KeyCode.T))
{
isAttached= true;
animator.SetBool(“Attachment”, true);
}
if (Input.GetKey(KeyCode.Y))
{
isAttached = false;
animator.SetBool(“Attachment”, false);
}
}
}

And I will also add some screeenshots of the animator of my gun.

Your post does not relate to the Editor itself, it looks like a scripting question so it’s best asked on the Scripting forum. I’ll move your post there.

Note that there’s also a dedicated Animation forum.

Also, please edit your post to use code-tags as nobody is likely to wade through a wall of plain text. :wink:

Here is how to begin your debugging journey in order to fix your problem:

You must find a way to get the information you need in order to reason about what the problem is.

Once you understand what the problem is, you may begin to reason about a solution to the problem.

What is often happening in these cases is one of the following:

  • the code you think is executing is not actually executing at all
  • the code is executing far EARLIER or LATER than you think
  • the code is executing far LESS OFTEN than you think
  • the code is executing far MORE OFTEN than you think
  • the code is executing on another GameObject than you think it is
  • you’re getting an error or warning and you haven’t noticed it in the console window

To help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

Doing this should help you answer these types of questions:

  • is this code even running? which parts are running? how often does it run? what order does it run in?
  • what are the values of the variables involved? Are they initialized? Are the values reasonable?
  • are you meeting ALL the requirements to receive callbacks such as triggers / colliders (review the documentation)

Knowing this information will help you reason about the behavior you are seeing.

You can also supply a second argument to Debug.Log() and when you click the message, it will highlight the object in scene, such as Debug.Log("Problem!",this);

If your problem would benefit from in-scene or in-game visualization, Debug.DrawRay() or Debug.DrawLine() can help you visualize things like rays (used in raycasting) or distances.

You can also call Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene manually, looking for all the parts, where they are, what scripts are on them, etc.

You can also call GameObject.CreatePrimitive() to emplace debug-marker-ish objects in the scene at runtime.

You could also just display various important quantities in UI Text elements to watch them change as you play the game.

If you are running a mobile device you can also view the console output. Google for how on your particular mobile target, such as this answer or iOS: https://discussions.unity.com/t/700551 or this answer for Android: https://discussions.unity.com/t/699654

If you are working in VR, it might be useful to make your on onscreen log output, or integrate one from the asset store, so you can see what is happening as you operate your software.

Another useful approach is to temporarily strip out everything besides what is necessary to prove your issue. This can simplify and isolate compounding effects of other items in your scene or prefab.

Here’s an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong:

https://discussions.unity.com/t/839300/3

When in doubt, print it out!™

Note: the print() function is an alias for Debug.Log() provided by the MonoBehaviour class.

Once you find more out about the problem, here is how to report your problem productively in the Unity3D forums:

http://plbm.com/?p=220

This is the bare minimum of information to report:

  • what you want
  • what you tried
  • what you expected to happen
  • what actually happened, especially any errors you see
  • links to documentation you used to cross-check your work (CRITICAL!!!)

If you post a code snippet, ALWAYS USE CODE TAGS:

How to use code tags: https://discussions.unity.com/t/481379

You may edit your post above.