0
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GunController : MonoBehaviour
{
    public Transform muzzle;

    [SerializeField]
    private Gun currentGun;

    private float currentFireRate;

    private AudioSource audioSource;

    public float angle;

    public GameObject bulletObjA;
    public float BulletSpeed;

    public GameObject sPoint;

    SpriteRenderer rend;
    public float adjustment;

    void Start() 
    {
        rend = GetComponent<SpriteRenderer>();
    }

    // Update is called once per frame
    void Update()
    {
        RotateToMouseDir();
        GunFireRateCalc();
        TryFire();
    }

    void RotateToMouseDir()
    {
        Vector3 mouseWorldPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition + Vector3.forward * 10f);
        
        angle = Mathf.Atan2(
            this.transform.position.y - mouseWorldPosition.y,
            this.transform.position.x - mouseWorldPosition.x) * Mathf.Rad2Deg;

        float final =- (angle + 90f);

        this.transform.rotation = Quaternion.Euler(new Vector3(0f, 0f, -final + adjustment));

        if (angle > -90f && angle < 90f)
        {
            rend.flipY = true;
        }
        else if ((angle > 90f && angle < 180f) || (angle < -90f && angle > -180f))
        {
            rend.flipY = false;
        }

        if ((angle > -180f) && (angle < 0f))
        {
            this.gameObject.layer = 3;
        }
        else if ((angle > 0f) && (angle < 180f))
        {
            this.gameObject.layer = 5;
        }
    }

    private void GunFireRateCalc()
    {
        if (currentFireRate > 0)
        currentFireRate -= Time.deltaTime;
    }

    private void TryFire()
    {
        if(Input.GetButton("ShootMain") && currentFireRate <= 0)
        {
            Fire();
        }
        else if(!Input.GetButton("ShootMain") && currentFireRate != 0)
        {
            currentFireRate = 0;
        }
    }

    private void Fire()
    {
        currentFireRate = currentGun.fireRate;
        Shoot();
    }

    private void Shoot()
    {
        GameObject bullet = Instantiate(bulletObjA, muzzle.position, Quaternion.Euler(new Vector3(0f, 0f, angle + 180)));
        Rigidbody2D rigid = bullet.GetComponent<Rigidbody2D>();
        rigid.AddRelativeForce(Vector2.right * BulletSpeed, ForceMode2D.Impulse);

        Debug.Log("총알 발사 함");
    }
}

This is my main code and the second one is Gun.cs under.

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

public class Gun : MonoBehaviour
{
    public string gunName;
    public float range;
    public float accuracy;
    public float fireRate = 5;
    public float reloadTime = 4;

    public int damage;

    public int reloadBulletCount;
    public int currentBulletCount;
    public int maxBulletCount;
    public int carryBulletCount;

    public Vector3 findSightORiginalPos; //정조준시 총이 향할 위치. 정조준 할 때 총의 위치가 변하니까 그 때의 위치

    public Animator anim;
    public ParticleSystem muzzleFlash;
    public AudioClip fire_Sound;
}

It seems like the main code is having issues with references. The error message doesn't appear until I actually try firing. After clicking the fire button, nothing happens.

NullReferenceException: Object reference not set to an instance of an object
GunController.Fire () (at Assets/Scripts/Weapon/GunController.cs:90)
GunController.TryFire () (at Assets/Scripts/Weapon/GunController.cs:80)
GunController.Update () (at Assets/Scripts/Weapon/GunController.cs:36)

This is the error code I recieved. I figured out that the problem was within Gun.cs script but I couldn't find out how to figure it out.

Jagari
  • 7
  • 1
  • This is a Null Reference error Right Did you checked whether you have dragged and dropped the Gun Script in Field of the Main Script? It should Work then. – Selvaraj Balakrishnan Jan 30 '23 at 12:14

0 Answers0