Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Use of unassigned local variable "bullet"

Discussion in 'Scripting' started by HumphreyGames, Oct 22, 2021.

  1. HumphreyGames

    HumphreyGames

    Joined:
    May 5, 2020
    Posts:
    40
    Hi so I have a shooting script and a bullet prefab inside of that.

    The code for the script is here:
    Code (CSharp):
    1. using System;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class Shooting : MonoBehaviour
    7. {
    8.     [Header("Gun Stats")]
    9.     public int damage;
    10.     public float timeBetweenShooting, range, reloadTime, timeBetweenShots, bulletForce;
    11.     public int magazineSize, bulletPerShot;
    12.     public bool allowButtonHold;
    13.     int bulletsLeft, bulletsShot;
    14.  
    15.     //Bools
    16.     [HideInInspector]public bool shooting, canShoot, reloading;
    17.  
    18.     [Header("References")]
    19.     public GameObject bullet;
    20.     private Transform firePoint;
    21.  
    22.     private void Awake()
    23.     {
    24.         bulletsLeft = magazineSize;
    25.         canShoot = true;
    26.     }
    27.  
    28.     private void Start()
    29.     {
    30.         firePoint = transform.Find("FirePoint");
    31.     }
    32.  
    33.     private void Update()
    34.     {
    35.         MyInput();
    36.     }
    37.  
    38.     private void MyInput()
    39.     {
    40.         if (allowButtonHold) shooting = Input.GetButton("Fire1");
    41.         else shooting = Input.GetButtonDown("Fire1");
    42.  
    43.         if (Input.GetKeyDown(KeyCode.R) && bulletsLeft < magazineSize && !reloading) Reload();
    44.  
    45.         //Shoot
    46.         if (canShoot && shooting && !reloading && bulletsLeft > 0)
    47.         {
    48.             bulletsShot = bulletPerShot;
    49.             Shoot();
    50.         }
    51.     }
    52.  
    53.     private void Reload()
    54.     {
    55.         reloading = true;
    56.         Invoke("ReloadFinished", reloadTime);
    57.     }
    58.  
    59.     private void ReloadFinished()
    60.     {
    61.         bulletsLeft = magazineSize;
    62.         reloading = false;
    63.     }
    64.  
    65.     void Shoot()
    66.     {
    67.         canShoot = false;
    68.  
    69.         GameObject bullet = Instantiate(bullet, firePoint.position, firePoint.rotation);
    70.         Rigidbody2D rb = bullet.GetComponent<Rigidbody2D>();
    71.         rb.AddForce(firePoint.transform.right * bulletForce, ForceMode2D.Impulse);
    72.  
    73.         bulletsLeft--;
    74.         bulletsShot--;
    75.         Invoke("ResetShot", timeBetweenShooting);
    76.  
    77.         if (bulletsShot > 0 && bulletsLeft > 0)
    78.             Invoke("Shoot", timeBetweenShots);
    79.     }
    80.  
    81.     private void ResetShot()
    82.     {
    83.         canShoot = true;
    84.     }
    85. }
    At this line:
    Code (CSharp):
    1. GameObject bullet = Instantiate(bullet, firePoint.position, firePoint.rotation);
    , I am getting an error saying "Use of unassigned local variable "bullet".

    I have assigned the prefab in the inspector so I don't know what is going on. Any help?
     
  2. HumphreyGames

    HumphreyGames

    Joined:
    May 5, 2020
    Posts:
    40
    never mind I fixed it. Stupid mistake. i have 2 variables with the same name