Search Unity

Error CS0165 "Use of unassigned local variable" on 2 variables, how can i fix it?

Discussion in 'Scripting' started by ProgramandoHistorias, Jun 23, 2020.

  1. ProgramandoHistorias

    ProgramandoHistorias

    Joined:
    Apr 1, 2020
    Posts:
    11
    I had multiple problems with my recoiling script, and all of them was fixed expect this one. How can i fix it?| Assets\AdvancedWeaponRecoil.cs(42,42): error CS0165: Use of unassigned local variable 'rotationalRecoil'
    Assets\AdvancedWeaponRecoil.cs(43,42): error CS0165: Use of unassigned local variable 'positionalRecoil'

    Line 42
    Code (CSharp):
    1. rotationalRecoil = Vector3.Slerp(rotationalRecoil, Vector3.zero, rotationalReturnSpeed* Time.deltaTime);
    Line 43
    Code (CSharp):
    1.  positionalRecoil = Vector3.Slerp(positionalRecoil, Vector3.zero, positionalReturnSpeed* Time.deltaTime);
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    You need to add positionalRecoil and rotationalRecoil as class variables. e.g.:
    Code (csharp):
    1. public class YourClass : MonoBehaviour {
    2. Vector3 positionalRecoil;
    3. Vector2 rotationalRecoil;
    4. ...
     
  3. ProgramandoHistorias

    ProgramandoHistorias

    Joined:
    Apr 1, 2020
    Posts:
    11
    You are awesome! I finally fixed it all! Thanks a lot!