Search Unity

Error CS0103 on 3 different functions.

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

  1. ProgramandoHistorias

    ProgramandoHistorias

    Joined:
    Apr 1, 2020
    Posts:
    11
    I am planning on doing a FPS-like game and i've decided to do some recoil for my weapons, but when i saved it i got three CS0103 errors and i can't find out what to do. Can someone help me? Here is the code:
    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class AdvancedWeaponRecoil : MonoBehaviour
    7. {
    8.     [Header("Reference Points:")]
    9.     public Transform recoilPosition;
    10.     public Transform rotationPoint;
    11.     [Space(10)]
    12.  
    13.     [Header("Speed Settings:")]
    14.     public float positionalRecoilSpeed = 8f;
    15.     public float rotationalRecoilSpeed = 8f;
    16.     [Space(10)]
    17.  
    18.     public float positionalReturnSpeed = 18f;
    19.     public float rotationalReturnSpeed = 38f;
    20.     [Space(10)]
    21.  
    22.     [Header("Amount Settings:")]
    23.     public Vector3 RecoilRotation = new Vector3(10, 5, 7);
    24.     public Vector3 RecoilKickBack = new Vector3(0.005f, 0f, -0.2f);
    25.     [Space(10)]
    26.     public Vector3 RecoilRotationAim = new Vector3(10, 4, 6);
    27.     public Vector3 RecoilKickBackAim = new Vector3(0.015f, 0f, -0.2f);
    28.     [Space(10)]
    29.  
    30.     Vector3 rotationalRecoil;
    31.     Vector3 positionalRecoil;
    32.     Vector3 Rot;
    33.     [Header("State:")]
    34.     public bool aiming;
    35.  
    36.     void FixedUpdate()
    37.     {
    38.         rotationalRecoil = Vector3.Lerp(rotacionalRecoil, Vector3.zero, rotationalReturnSpeed * Time.deltaTime);
    39.         positionalRecoil = Vector3.Lerp(posicionalRecoil, Vector3.zero, positionalReturnSpeed * Time.deltaTime);
    40.  
    41.         recoilPosition.localPosition = Vector3.Slerp(recoilPosition, localPosition, positionalRecoil, positionalRecoilSpeed * Time.fixedDeltaTime);
    42.         Rot = Vector3.Slerp(Rot, rotationalRecoil, rotationalRecoilSpeed * Time.fixedDeltaTime);
    43.         rotationPoint.localRotation = Quaternion.Euler(Rot);
    44.     }
    45.  
    46.     void Update()
    47.     {
    48.         if (Input.GetButtonDown("Shoot"))
    49.         {
    50.             Shoot();
    51.         }
    52.         if (Input.GetButtonDown("Aim"))
    53.         {
    54.             aiming = true;
    55.         }
    56.         else
    57.         {
    58.             aiming = false;
    59.         }
    60.     }
    61.  
    62.     public void Shoot()
    63.     {
    64.         if (aiming)
    65.         {
    66.             rotationalRecoil += new Vector3(-RecoilRotationAim.x, Random.Range(-RecoilRotationAim.y, RecoilRotationAim.y), Random.Range(-RecoilRotationAim.z, RecoilRotationAim.z));
    67.             positionalRecoil += new Vector3(Random.Range(-RecoilKickBackAim.x, RecoilKickBackAim.x), Random.Range(-RecoilKickBackAim.y, RecoilKickBackAim.y), (RecoilKickBackAim.z));
    68.         }
    69.         else
    70.         {
    71.             rotationalRecoil += new Vector3(-RecoilRotation.x, Random.Range(-RecoilRotation.y, RecoilRotation.y), Random.Range(-RecoilRotationAim.z, RecoilRotationAim.z));
    72.             positionalRecoil += new Vector3(Random.Range(-RecoilKickBack.x, RecoilKickBack.x), Random.Range(-RecoilKickBack.y, RecoilKickBack.y), RecoilKickBack.z);
    73.         }
    74.        
    75.     }
    76. }
    77.  
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,909
    The error message has important information in it like a description, and line and column numbers. Please include the entire error message. Nobody remembers random error numbers off the top of their head.
     
    Vryken and Joe-Censored like this.
  3. ProgramandoHistorias

    ProgramandoHistorias

    Joined:
    Apr 1, 2020
    Posts:
    11
    error CS0103: The name 'rotacionalRecoil' does not exist in the current context | error CS0103: The name 'localPosition' does not exist in the current context | error CS0103: The name 'posicionalRecoil' does not exist in the current context | Here.
     
  4. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,909
    Read those error messages carefully. Compare the variable names to the ones you declared. You've made some typos. In the "localPosition" case you never declared a variable called "localPosition" at all.
     
  5. ProgramandoHistorias

    ProgramandoHistorias

    Joined:
    Apr 1, 2020
    Posts:
    11

    float localPosition;

    i added this and now i got error CS1501 : no overload for method Slerp takes 4 arguments. I tried it with Vector3 and got the same error.
     
  6. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,909
  7. ProgramandoHistorias

    ProgramandoHistorias

    Joined:
    Apr 1, 2020
    Posts:
    11
  8. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,909
    You can use Slerp, you just have to provide it the correct arguments. Look at the link I posted to see what those are.
     
  9. ProgramandoHistorias

    ProgramandoHistorias

    Joined:
    Apr 1, 2020
    Posts:
    11
    Oh ok, fixed it. But i got another error. error CS1503: Argument 1: cannot convert from 'UnityEngine.Transform' to 'UnityEngine.Vector3' i just changed this code and its mentioning the recoilPosition part if im not wrong,how can i fix it?
    Code (CSharp):
    1. recoilPosition.localPosition = Vector3.Slerp(recoilPosition, positionalRecoil, positionalRecoilSpeed * Time.fixedDeltaTime);
     
  10. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,909
  11. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,735
    Please stop reporting errors by number. NOBODY knows that crap.

    How to report problems productively in the Unity3D forums:

    http://plbm.com/?p=220

    Help us to help you.
     
    Joe-Censored likes this.
  12. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    You need to look at the documentation for this stuff and pay attention to what variable types you are providing. In programming everything has to be EXACTLY correct. The computer will not just figure out what you mean.

    In tihs case Slerp takes:
    https://docs.unity3d.com/ScriptRefe...38.857441054.1592847615-1494478386.1583775254

    So you need to provide it two Vector3's and a float. The first argument you provide it though is recoilPosition. Take a look at your code you posted at the top where you declare recoilPosition on line 9. You declare is as type Transform, not Vector3. Maybe you meant recoilPosition.position, which actually is a Vector3, maybe you mean recoilPosition.localPosition, which is also a Vector3, I don't actually know what you're doing here. Just you need to pay attention to the details or you're going to have a bad time with programming. The details are what matters.
     
  13. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,735
    Software is never 99% correct. Software has to be 100% correct. That's how it works. Spelling, capitalization, punctuation, semantic meaning, EVERYTHING has to be there.
     
    Joe-Censored likes this.
  14. ProgramandoHistorias

    ProgramandoHistorias

    Joined:
    Apr 1, 2020
    Posts:
    11
    Thanks for the help.
     
  15. ProgramandoHistorias

    ProgramandoHistorias

    Joined:
    Apr 1, 2020
    Posts:
    11
    i just need to figure out how to solve the CS0103 errors (the name '____' does not exist in the current context) on rotationalRecoil and positionalRecoil. How can i solve it?
     
  16. Vryken

    Vryken

    Joined:
    Jan 23, 2018
    Posts:
    2,106
    A little lesson on scope:
    Variables you declare only exist within the curly braces
    { }
    you declare them in.

    When you declare a variable within the braces of a class, it's considered a global variable that can be used anywhere within the class:
    Code (CSharp):
    1. public class MyClass {
    2.    string myString;
    3.  
    4.    void MyFirstMethod() {
    5.       myString = "Hello"; // Valid
    6.    }
    7.  
    8.    void MySecondMethod() {
    9.       myString = "World"; // Valid
    10.    }
    11. }
    When you declare a variable within the braces of a method, it's considered a local variable that can only be used within that method. If you try to use it anywhere else, that's when error CS0103 will be thrown:
    Code (CSharp):
    1. public class MyClass {
    2.    string myString;
    3.  
    4.    void MyFirstMethod() {
    5.       myString = "Hello"; // Valid
    6.  
    7.       int myLocalInt = 1; // Valid
    8.    }
    9.  
    10.    void MySecondMethod() {
    11.       myString = "World"; // Valid
    12.  
    13.       myLocalInt = 2;
    14.       // ^ Error CS0103: The name "myLocalInt" does not exist in the current context.
    15.       // The variable only exists within "MyFirstMethod".
    16.    }
    17. }
    The same is true when you declare variables within the braces of if/else/try/catch/finally/switch/while/for/foreach/etc. statements. They can only be accessed within the statement:
    Code (CSharp):
    1.  
    2. void ScopeExample() {
    3.    bool continueLoop = true;
    4.    int elapsedLoops = 0;
    5.  
    6.    while(continueLoop) {
    7.       int currentLoop = elapsedLoops++; // Valid
    8.  
    9.       if(currentLoop > 5) {
    10.          continueLoop = false; // Valid
    11.       }
    12.    }
    13.  
    14.    currentLoop = 0;
    15.    // ^ Error CS0103: The name "currentLoop" does not exist in the current context.
    16.    // The variable only exists within the curly braces of the above while-loop.
    17. }
     
    Last edited: Jun 23, 2020
    Kurt-Dekker likes this.
  17. ProgramandoHistorias

    ProgramandoHistorias

    Joined:
    Apr 1, 2020
    Posts:
    11
    Thanks about that, it helps me a lot.