0
\$\begingroup\$

I was just trying to set up an array through Unity when this happened. Basically, I set the array size and values through the Unity Inspector, however, it did not register in VS. I found this out when doing Debug.Log(array);, which returned an array with 0 items, even though I set it to 8 in Unity.

This also happened with Gameobjects, floats, and pretty much every parameter I tried to assign through Unity or inside the script itself at the top. This has only started happening today, so almost all of my variables work just fine, however, the new ones I added recently have the same problem.

It could be to do with how many variables I have in this script, however from my experience there isn't a cap, nor did I find anyone else with this problem.

here is my code:

I decided to include most of it since I have virtually no idea what the problem is...

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class Shooter : MonoBehaviour
{
    [SerializeField] private GameObject bulletImpact;
    [SerializeField] private LayerMask IgnoreBullet;
    [SerializeField] private GameObject Crosshair;
    [SerializeField] private Transform pivot;
    [SerializeField] private Animator anim;

    [SerializeField] private float rayDistance;
    [SerializeField] private float recoilAng, recoilKb;

    [SerializeField] private float bulletsTillCd = 8;
    [SerializeField] private float coolDown = 0.01f;
    [SerializeField] private float cdTime = 2;

    [SerializeField] private PlayerController playerController;
    [SerializeField] private BoxCollider _collider;
    [SerializeField] private GameObject shooter;

    private Vector3 kb;
    private Transform mainCam;
    private GameObject touching;
    private Rigidbody rb;
    private Slider cdDisplay;

    private float bulletsShot;
    private bool isShooting = true;
    private bool nearWall = false;
    private bool isCooling = false;

    [SerializeField] private Vector3[] recoilArray;

    private void Start()
    {
        rb = GetComponent<Rigidbody>();
        mainCam = Camera.main.transform;
        cdDisplay = GameObject.Find("CoolDown").GetComponent<Slider>();
    }

    private void Update()
    {
        if (isShooting == false && bulletsShot > 0)
        {
            bulletsShot -= 0.01f;
            cdDisplay.value = bulletsShot;
            isCooling = true;
        } else if (bulletsShot <= 0)
        {
            isCooling = false;
        }
    }

    public void Shoot()
    {
        if (bulletsShot <= 7 && isCooling == false)
        {
            audioScript.PlaySound("gunshot");
            anim.Play("Base Layer.shooting", 0, 0);

            bulletsShot += 1;
            StartCoroutine(cdTimer());
            isShooting = true;
            cdDisplay.value = bulletsShot;
            Debug.Log(recoilArray);

            RaycastHit hit;
            if (Physics.Raycast(shooter.transform.position , shooter.transform.up + recoilArray[(int)bulletsShot - 1], out hit, rayDistance, IgnoreBullet))
            {
                Instantiate(bulletImpact, hit.point + hit.normal * 0.0001f, Quaternion.LookRotation(-hit.normal));
            }
            Kb();
        }

    }

    private IEnumerator cdTimer()
    {
        float bullets = bulletsShot;

        yield return new WaitForSeconds(2);

        if (bullets == bulletsShot)
        {
            isShooting = false;
        }
    }
}

It is obviously a massive issue, I would really appreciate if someone could help me fix this!

\$\endgroup\$
1
  • \$\begingroup\$ If you're getting different results from Debug.Log than what you see in the Inspector, that's often a sign that you have more than one instance of this script. One that you've configured in the Inspector, and another one that you didn't — maybe a copy you created accidentally or forgot to clean up. I find logging the name of the object and its path in the scene hierarchy can be a good way to check for this. \$\endgroup\$
    – DMGregory
    Apr 30 at 21:26

0

You must log in to answer this question.

Browse other questions tagged .