Search Unity

error CS0161: not all code paths return a value,

Discussion in 'Scripting' started by mrstar55, Mar 13, 2016.

  1. mrstar55

    mrstar55

    Joined:
    Nov 19, 2015
    Posts:
    2
    I have a problem with small code: error CS0161: not all code paths return a value. I'm a beginner in coding and please help me.

    Code (CSharp):
    1.      public static float axisH = 0.0f;
    2.      public static float axisV = 0.0f;
    3.    
    4.      void start(){
    5.    
    6.      float f =  InputGetAxis ("Horizontal") ;
    7.      /...
    8.    
    9.      }
    10.    
    11.    
    12.      float InputGetAxis (string axis){
    13.    
    14.    
    15.              float v = Input.GetAxis(axis);
    16.    
    17.              if ((Mathf.Abs(v)) > 0.005f) return v ;
    18.              else if     (axis =="Horizontal" ) return axisH ;
    19.              else if (axis == "Vertical" ) return axisV ;
    20.          }
     
  2. Polymorphik

    Polymorphik

    Joined:
    Jul 25, 2014
    Posts:
    599
    you are missing an else
     
    Kiwasi likes this.
  3. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    The code is comparing that you don't always return a value. One way to get around this is just to put a return 0 at the bottom of the code.

    Edit: Think through what would happen if v was 1 and axis was "cat"
     
    mrstar55 likes this.