Search Unity

error CS0165: Use of unassigned local variable 'Bullet'

Discussion in 'Scripting' started by left4kill5, Sep 18, 2020.

  1. left4kill5

    left4kill5

    Joined:
    Sep 17, 2020
    Posts:
    12
    Hello, I am in need of some help. This makes no sense to me. Can anyone help me fix it? A quick scan through and I can't seem to find where I added a local variable. Thanks, I am pretty new to coding. The goal I'm trying to complete is shooting out bullets. For some reason though, the bullets don't actually move.

    [
    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class Shooting : MonoBehaviour
    7. {
    8.     public Transform firePoint;
    9.     public GameObject bulletPrefab;
    10.    
    11.     public float bulletForce = 20f;
    12.     // Update is called once per frame
    13.     void Update()
    14.     {
    15.         if(Input.GetButtonDown("Fire1"))
    16.         {
    17.             Shoot();
    18.         }
    19.     }
    20.    
    21.     void Shoot()
    22.     {
    23.        
    24.     GameObject bullet = Instantiate(bulletPrefab, firePoint.position, firePoint.rotation);
    25.     Rigidbody2D rb = bullet.GetComponent<Rigidbody2D>();
    26.     rb.AddForce(firePoint.up * bulletForce, ForceMode2D.Impulse);
    27.     }
    28. }
    29.  
    30.  
    31.  
    32.  
    33. }
     
    Last edited: Sep 18, 2020
  2. left4kill5

    left4kill5

    Joined:
    Sep 17, 2020
    Posts:
    12
    Sorry, I fixed the local variable bullet. The problem now is making the bullet move
     
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,700
    You may get what you want by adding a force to the bullet, but I prefer to just move it myself with a Ballistic script. It makes it far easier to control behaviors. Here's my go-to ballistic script:

    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Ballistic : MonoBehaviour
    5. {
    6.     // set this to desired initial speed - once.
    7.     public Vector3 velocity;
    8.  
    9.     void Update ()
    10.     {
    11.         // calculate  bullet drop
    12.         velocity += Physics.gravity * Time.deltaTime;
    13.  
    14.         // move the bullet
    15.         transform.position += velocity * Time.deltaTime;
    16.     }
    17. }
    Just add one of those to your bullet, set up the velocity and off it goes.

    Because bullets go so fast, I like to use a raycast-ahead method to make sure they don't pass through thin objects like telephone poles or whatenot.
     
    tmanallen likes this.
  4. left4kill5

    left4kill5

    Joined:
    Sep 17, 2020
    Posts:
    12
    Thanks!
     
    Kurt-Dekker likes this.
  5. left4kill5

    left4kill5

    Joined:
    Sep 17, 2020
    Posts:
    12
    If you don't mind, I have another script will like to get helped on. My errors here are

    Assets\Script\Player 2\Meleep2.cs(12,35): error CS1026: ) expected

    Assets\Script\Player 2\Meleep2.cs(12,27): error CS1010: Newline in constant

    However, I have created 2 of the same scripts for another player, but they don't have these errors. Sorry if I troubled you enough.


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class Meleep2 : MonoBehaviour
    7. {
    8.     public void OnCollisionEnter(Collision col)
    9.     {
    10.         if(col.GameObject.name == "Player")
    11.         {
    12.             if(Input.GetButtonDown("Fire1))
    13.            {
    14.                GameObject.find(Player).GetComponent<health1>().TakeDamage(20);
    15.            }
    16.          
    17.          
    18.        }
    19.    }
    20. }
     
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,700
    Pay attention to punctuation AND capitalization!!!!!

    Find is spelled with an uppercase F

    "Player" needs to be in quotes

    ALSO... I highly recommend not coding all the things on one line like this because (as your post indicates) it becomes impossible to reason about it.

    Instead, break it up into one thing per line. Here's how:

    How to break down hairy lines of code:

    http://plbm.com/?p=248
     
  7. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,993
    In addition pay more attention to your code. Don't you use a proper IDE like Visual Studio? In this line:
    you didn't close the string
    "Fire1"
    . So you're missing the closing quotation mark.

    All errors I see are
    • using System.Collections;
      You have this using statements two times. Remove one.
    • col.GameObject.name ==
      : GameObject should be gameObject. You want to access the gameObject property.
    • if(Input.GetButtonDown("Fire1))
      : As mentioned, the closing quote is missing.
    • GameObject.find(
      : Method names usually start with a capital letter in C#. So you have to use Find instead of find.
    • GameObject.Find(Player)
      The Find method expects a string value. So if you're looking for a gameobject named "Player" you have to put quotation marks around Player to create a string literal.
    • Using
      Input.GetButtonDown(
      inside OnCollisionEnter makes no sense and doesn't work. OnCollisionEnter is run once at the frame when the two objects start colliding. GetButtonDown is also only true for one frame when the button is pressed down. It's almost impossible to have those two events happen at the exact same frame.
    ps: Since you have this check:
    if(col.GameObject.name == "Player")
    in your code, it seems strange that you use GameObject.Find to find the player object when you already have the player object reference(i.e.
    col.gameObject
    ).

    Finally not an actual error but just an inconsistency. Class names should always start with a captial letter. You seem to have named your class
    health1
     
  8. left4kill5

    left4kill5

    Joined:
    Sep 17, 2020
    Posts:
    12
    Thanks. I am very sorry if I caused anymore trouble
     
  9. Tiny_Tree

    Tiny_Tree

    Joined:
    Oct 20, 2021
    Posts:
    6
    I have a similar problem. I get the error: error CS0165: Use of unassigned local variable 'SmallFly'
    Could any one explain why?

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class FlySpawner : MonoBehaviour
    6. {
    7.     public GameObject SmallFly;
    8.     // Start is called before the first frame update
    9.     void Start()
    10.     {
    11.     }
    12.  
    13.     // Update is called once per frame
    14.     void Update()
    15.     {
    16.         SpawnSmallFly(1);
    17.     }
    18.  
    19.     void SpawnSmallFly(int flyNum)
    20.     {
    21.         for (int i = 0; i < flyNum; i++)
    22.         {
    23.             GameObject SmallFly = Instantiate(SmallFly, new Vector3 (SmallFly.transform.position.x, SmallFly.transform.position.y, i), SmallFly.transform.rotation);
    24.         }
    25.     }
    26. }
     
  10. ThermalFusion

    ThermalFusion

    Joined:
    May 1, 2011
    Posts:
    906
    Please make your own thread instead of reviving a thread that is several years old.
    You have named two variables the same 'SmallFly' which is why you get this error. Give them unique names.
     
    Bunny83 likes this.